Queue.Enqueue() Method in C#
This method is used to add an object to the end of the Queue. This comes under the System.Collections namespace. The value can null and if the Count is less than the capacity of the internal array, this method is an O(1) operation. If the internal array needs to be reallocated to accommodate the new element, this method becomes an O(n) operation, where n is Count.
Syntax :
public virtual void Enqueue (object obj);
Here, obj is the object which is added to the Queue.
Example:
CSHARP
using System;
using System.Collections;
class GFG {
public static void Main()
{
Queue myQueue = new Queue();
myQueue.Enqueue( "one" );
Console.Write( "Total number of elements in the Queue are : " );
Console.WriteLine(myQueue.Count);
myQueue.Enqueue( "two" );
Console.Write( "Total number of elements in the Queue are : " );
Console.WriteLine(myQueue.Count);
myQueue.Enqueue( "three" );
Console.Write( "Total number of elements in the Queue are : " );
Console.WriteLine(myQueue.Count);
myQueue.Enqueue( "four" );
Console.Write( "Total number of elements in the Queue are : " );
Console.WriteLine(myQueue.Count);
myQueue.Enqueue( "five" );
Console.Write( "Total number of elements in the Queue are : " );
Console.WriteLine(myQueue.Count);
myQueue.Enqueue( "six" );
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
Reference:
Last Updated :
13 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...