Advertisement



< Prev
Next >



C# Queue





In C#, Queue is a non-generic collection class which is used to maintain a first-in, first-out(FIFO) collection of objects i.e. the first element to enter the Queue is the first one to be removed from it.

In other words, the queue can be thought of as people waiting in a queue to enter a shopping complex, the first person at the front of the queue is the first one to exit the queue and enter the shopping complex, while a new person is always added at the back/end of the queue.

Note: Queue implements the ICollection, IEnumerable, and ICloneable interfaces.



Important features of a Queue







Commonly used constructors of Queue


Constructor Description
Queue()
This constructor creates an empty instance of the Queue class by using the default initial capacity.

The capacity of a Queue is the number of elements it can hold, as the new elements are added to the Queue, its capacity is automatically increased.

Queue(int capacity)
This constructor initializes created a Queue that is empty and has the specified initial capacity.

Queue(ICollection)
This constructor creates a Queue initialized with the elements of an ICollection.






Properties of Queue:


Properties Description
Count
This property gives total number of elements in a Queue.

IsSynchronzied
This property determines whether a Queue is thread-safe or not.

SyncRoot
Thie property gets an object that can be used to synchronize access to the Queue.






Some commonly used methods of Queue class


Methods Description
Object Dequeue()
This method removes and returns the object at the front/beginning of the Queue.

void Enqueue(Object ob)
This method inserts an object at end of the Queue.

Object Peek()
This method returns the object at the top of the Queue without removing it.

bool Contains(Object o)
This method checks if Queue contains a specific Object.

void Clear()
This method removes all the elements from the Queue.

IEnumerator GetEnumerator()
This method returns an IEnumerator to enumerate the entire Queue.

Object[ ] ToArray()
This method returns an Object array containing all the elements of a Queue.




Advertisement




An example of Queue


In this program we have created a Queue to hold objects and we will add and remove objects from this Queue using its methods described above in the table. Let's see the code now -:

//C# Example of a Queue

using System;
using System.Collections;

class Queue1
{
public static void Main(String[] ar)
{
	//Creating a Queue
	Queue st= new Queue();
	

	//Calling the Enqueue() method to push elements into the Queue
	//New element is always added to the end of the Queue
	st.Enqueue(10);
	st.Enqueue(23);
	st.Enqueue(16);
	st.Enqueue(5);
	st.Enqueue(29);

	//Printing the contents of Queue
	Console.WriteLine("The contents of Queue: ");
	foreach(int element in st)
		Console.WriteLine(element);


	//Calling the Dequeue() method to remove the front element of the Queue
	Console.WriteLine("Removing the front element = "+ st.Dequeue());
	Console.WriteLine("Removing the next front element = "+ st.Dequeue());


	//Calling the Peek() method which only gets the front element of the Queue, without removing it
	Console.WriteLine("Peeking at the current front element = "+ st.Peek());


	//Printing the updated contents of Queue
	Console.WriteLine("Updated contents of Queue: ");
	foreach(int element in st)
		Console.WriteLine(element);
}
}


Output is - :


The contents of Queue:
10
23
16
5
29
Removing the front element = 10
Removing the next front element = 23
Peeking at the front element = 16
Updated contents of Queue:
16
5
29


Program Analysis










Converting a Queue to an Object array


In the upcoming program, we will call the ToArray(), which returns an Object array containing all the elements of the invoked Stack.

//C# Example of a Queue

using System;
using System.Collections;

class Queue1
{
public static void Main(String[] ar)
{
	//Creating a Queue
	Queue st= new Queue();
	

	//Calling the Enqueue() method to push elements into the Queue
	//New element is always added to the end of the Queue
	st.Enqueue(10);
	st.Enqueue(23);
	st.Enqueue(16);
	st.Enqueue(5);
	st.Enqueue(29);

	//Printing the contents of Queue
	Console.WriteLine("The contents of Queue: ");
	foreach(int element in st)
		Console.WriteLine(element);

	//Calling the ToArray() method of Queue
	Object[] obArr = st.ToArray();


	//Printing the contents of Object array
	Console.WriteLine("The contents of Object array initialized from the elements of Queue: ");
	foreach(Object element in obArr)
		Console.WriteLine(element);
}
}


Output is :


The contents of Queue:
10
23
16
5
29
The contents of Object array initialized from the elements of Queue:
10
23
16
5
29




Program Analysis






Please share this article -





< Prev
Next >
< C# Stack
C# SortedList >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement