Tutorialspoint.dev

C# | Queue Class

Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called dequeue . This class comes under System.Collections namespace and implements ICollection, IEnumerable, and ICloneable interfaces.

Characteristics of Queue Class:

  • Enqueue adds an element to the end of the Queue.
  • Dequeue removes the oldest element from the start of the Queue.
  • Peek returns the oldest element that is at the start of the Queue but does not remove it from the Queue.
  • The capacity of a Queue is the number of elements the Queue can hold.
  • As elements are added to a Queue, the capacity is automatically increased as required by reallocating the internal array.
  • Queue accepts null as a valid value for reference types and allows duplicate elements.

Constructors

Constructor Description
Queue() Initializes a new instance of the Queue class that is empty, has the default initial capacity, and uses the default growth factor.
Queue(ICollection) Initializes a new instance of the Queue class that contains elements copied from the specified collection, has the same initial capacity as the number of elements copied, and uses the default growth factor.
Queue(Int32) Initializes a new instance of the Queue class that is empty, has the specified initial capacity, and uses the default growth factor.
Queue(Int32, Single) Initializes a new instance of the Queue class that is empty, has the specified initial capacity, and uses the specified growth factor.

Example:



// C# code to create a Queue
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue 
        Queue myQueue = new Queue();
  
        // Inserting the elements into the Queue
        myQueue.Enqueue("one");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
  
        myQueue.Enqueue("two");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
  
        myQueue.Enqueue("three");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
  
        myQueue.Enqueue("four");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
  
        myQueue.Enqueue("five");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
  
        myQueue.Enqueue("six");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
    }
}

Output:

Total number of elements in the Queue are : 1
Total number of elements in the Queue are : 2
Total number of elements in the Queue are : 3
Total number of elements in the Queue are : 4
Total number of elements in the Queue are : 5
Total number of elements in the Queue are : 6

Properties

Property Description
Count Gets the number of elements contained in the Queue.
IsSynchronized Gets a value indicating whether access to the Queue is synchronized (thread safe).
SyncRoot Gets an object that can be used to synchronize access to the Queue.

Example:

// C# code to Get the number of
// elements contained in the Queue
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue
        Queue myQueue = new Queue();
  
        // Inserting the elements into the Queue
        myQueue.Enqueue("Chandigarh");
        myQueue.Enqueue("Delhi");
        myQueue.Enqueue("Noida");
        myQueue.Enqueue("Himachal");
        myQueue.Enqueue("Punjab");
        myQueue.Enqueue("Jammu");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
    }
}

Output:

Total number of elements in the Queue are : 6

Methods

Method Description
Clear() Removes all objects from the Queue.
Clone() Creates a shallow copy of the Queue.
Contains(Object) Determines whether an element is in the Queue.
CopyTo(Array, Int32) Copies the Queue elements to an existing one-dimensional Array, starting at the specified array index.
Dequeue() Removes and returns the object at the beginning of the Queue.
Enqueue(Object) Adds an object to the end of the Queue.
Equals(Object) Determines whether the specified object is equal to the current object.
GetEnumerator() Returns an enumerator that iterates through the Queue.
GetHashCode() Serves as the default hash function.
GetType() Gets the Type of the current instance.
MemberwiseClone() Creates a shallow copy of the current Object.
Peek() Returns the object at the beginning of the Queue without removing it.
Synchronized(Queue) Returns a new Queue that wraps the original queue, and is thread safe.
ToArray() Copies the Queue elements to a new array.
ToString() Returns a string that represents the current object.
TrimToSize() Sets the capacity to the actual number of elements in the Queue.

Example 1:

// C# code to Check if a Queue
// contains an element
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue
        Queue myQueue = new Queue();
  
        // Inserting the elements into the Queue
        myQueue.Enqueue(5);
        myQueue.Enqueue(10);
        myQueue.Enqueue(15);
        myQueue.Enqueue(20);
        myQueue.Enqueue(25);
  
        // Checking whether the element is
        // present in the Queue or not
        // The function returns True if the
        // element is present in the Queue, else
        // returns False
        Console.WriteLine(myQueue.Contains(7));
    }
}

Output:

False

Example 2:

// C# code to Convert Queue to array
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue 
        Queue myQueue = new Queue();
  
        // Inserting the elements into the Queue
        myQueue.Enqueue("Geeks");
        myQueue.Enqueue("Geeks Classes");
        myQueue.Enqueue("Noida");
        myQueue.Enqueue("Data Structures");
        myQueue.Enqueue("GeeksforGeeks");
  
        // Converting the Queue
        // into object array
        Object[] arr = myQueue.ToArray();
  
        // Displaying the elements in array
        foreach(Object obj in arr)
        {
            Console.WriteLine(obj);
        }
    }
}

Output:

Geeks
Geeks Classes
Noida
Data Structures
GeeksforGeeks

Reference:



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter