Advertisement
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 a Queue, its capacity is automatically increased. |
Queue(int capacity) | This constructor creates 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 | 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 the access to a Queue. |
Methods | Description |
---|---|
Object Dequeue() | This method removes and returns an object at the front/beginning of a Queue. |
void Enqueue(Object ob) | This method inserts an object at end of a Queue. |
Object Peek() | This method returns an object at the top of a Queue, without removing it. |
bool Contains(Object o) | This method checks if a Queue contains a specific Object. |
void Clear() | This method removes all the elements from a Queue. |
IEnumerator GetEnumerator() | This method returns an IEnumerator to enumerate an entire Queue. |
Object[ ] ToArray() | This method returns an Object array, containing all the elements of a Queue. |
Advertisement
//C# Example of a Queue
using System;
using System.Collections;
class Queue1
{
public static void Main(String[] ar)
{
//Creating a Queue
Queue q = new Queue();
//Calling the Enqueue() method to push elements into the Queue
//New element is always added to the end of the Queue
q.Enqueue(10);
q.Enqueue(23);
q.Enqueue(16);
q.Enqueue(5);
q.Enqueue(29);
//Printing the contents of Queue
Console.WriteLine("The contents of Queue: ");
foreach(int element in q)
Console.WriteLine(element);
//Calling the Dequeue() method to remove the front element of the Queue
Console.WriteLine("Removing the front element = "+ q.Dequeue());
Console.WriteLine("Removing the next front element = "+ q.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 = "+ q.Peek());
//Printing the updated contents of Queue
Console.WriteLine("Updated contents of Queue: ");
foreach(int element in q)
Console.WriteLine(element);
}
}
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
//C# Example of a Queue
using System;
using System.Collections;
class Queue1
{
public static void Main(String[] ar)
{
//Creating a Queue
Queue q = new Queue();
//Calling the Enqueue() method to push elements into the Queue
//New element is always added to the end of the Queue
q.Enqueue(10);
q.Enqueue(23);
q.Enqueue(16);
q.Enqueue(5);
q.Enqueue(29);
//Printing the contents of Queue
Console.WriteLine("The contents of Queue: ");
foreach(int element in q)
Console.WriteLine(element);
//Calling the ToArray() method of Queue
Object[] obArr = q.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);
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement