Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to create a queue, then enqueue and dequeue elements in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Step 1: Create a queue of integers
        Queue<int> queue = new Queue<int>();

        // Step 2: Enqueue elements into the queue
        Console.WriteLine("Enqueuing elements...");
        queue.Enqueue(10);
        queue.Enqueue(20);
        queue.Enqueue(30);
        queue.Enqueue(40);

        // Step 3: Display the queue
        Console.WriteLine("Queue after enqueuing:");
        DisplayQueue(queue);

        // Step 4: Dequeue elements from the queue
        Console.WriteLine("\nDequeuing elements...");
        while (queue.Count > 0) {
            int dequeuedElement = queue.Dequeue();
            Console.WriteLine($"Dequeued: {dequeuedElement}");
        }

        // Step 5: Check if the queue is empty
        if (queue.Count == 0) {
            Console.WriteLine("\nThe queue is now empty.");
        }
    }

    // ???? Function to display queue contents
    static void DisplayQueue(Queue<int> queue) {
        foreach (int item in queue) {
            Console.WriteLine(item);
        }
    }
}




/*
run:

Enqueuing elements...
Queue after enqueuing:
10
20
30
40

Dequeuing elements...
Dequeued: 10
Dequeued: 20
Dequeued: 30
Dequeued: 40

The queue is now empty.

*/

 



answered Aug 22, 2025 by avibootz
...