Queue.Enqueue() Method In C#

The Queue.Enqueue function is utilized for appending an element to the back of the Queue data structure. A Queue follows the First-In-First-Out (FIFO) principle, meaning the initial element added is the first to be removed. Enqueue is a method of the Queue<T> class, with T representing the type of elements stored in the queue. It is part of the System.Collections package. This operation has a time complexity of O(1) when the Count is less than the array's capacity. However, if the internal array needs to be resized to accommodate the new element, the time complexity becomes O(n), where n is the Count.

When invoking the Enqueue method, you provide a parameter that represents the item you want to insert into the queue. Subsequently, this item is appended to the tail of the queue, positioning it as the final element in the sequence. In scenarios where the element collection is devoid of any items, this new element automatically becomes the sole entity in the queue. The time complexity associated with the Enqueue function is denoted as O(1), signifying a consistent time complexity that does not fluctuate with the queue's size. This characteristic allows for seamless addition of new elements to the queue without necessitating the traversal or reorganization of existing elements.

Syntax:

It has the following syntax:

Example

public virtual void Enqueue (object ob);

Here, the "ob" denotes the object that needs to be included.

Example:

Let's consider a scenario to demonstrate the Queue.Enqueue function in C#.

Example

using System; 
using System.Collections; 
class Enqueue{ 
    // Driver code 
    public static void Main() 
    { 
        // A queue is created 
        Queue myQue = new Queue(); 
        //The elements are inserted into the queue 
        myQue.Enqueue("one"); 
        // The print statement is used to display the number of items in the queue  
        Console.Write("The number of elements in the queue are:"); 
        
        Console.WriteLine(myQue.Count);     myQue.Enqueue("two"); 
        Console.Write("The number of elements in the queue are:");
        
        Console.WriteLine(myQue.Count);     myQue.Enqueue("three");        
        Console.Write("The number of elements in the queue are:"); 
        
        Console.WriteLine(myQue.Count); 
        myQue.Enqueue("four"); 
        // Displaying the count of elements contained in the Queue     
        Console.Write("The number of elements in the queue are:"); 
        
        Console.WriteLine(myQue.Count);     myQue.Enqueue("five");
        // Another print statement is used to display the number of items in the queue     
        Console.Write("The number of elements in the queue are:");
        
        Console.WriteLine(myQue.Count); 
        myQue.Enqueue("six"); 
        // The print statement to display the number of items in the queue     
        Console.Write("The number of elements in the queue are:"); 
        
        Console.WriteLine(myQue.Count); 
        } 
    
}

Output:

Output

The number of elements in the queue are:1
The number of elements in the queue are:2
The number of elements in the queue are:3
The number of elements in the queue are:4
The number of elements in the queue are:5
The number of elements in the queue are:6

Explanation:

  • Queue myQue = new Queue;: It returns a new instance of the Queue class called myQue.
  • Enqueue("element"): It returns a new instance of the Queue class named myQue. After that, elements are added to the queue. Use the Enqueue function to add items to the queue.
  • Write("The number of queue elements are: ");: It displays a message stating the number of queue items.
  • WriteLine(myQue.Count): It uses the Queue class's Count method to return the number of entries in the queue.
  • Advantages of Queue.Enqueue Method in C#

There are several advantages of the Queue.Enqueue method in C#. Some main advantages of this method are as follows:

  • FIFO (First-In-First-Out) Order: The FIFO method is used when enqueuing elements into a queue, which ensures that the first element entered is the first to be deleted. This sequence is especially beneficial in instances when maintaining order is critical.
  • Efficient Element Addition: Enqueuing items to a queue is very efficient ( O(1) in most circumstances), making it suited for applications requiring constant-time insertion, such as breadth-first searchers or task scheduling.
  • Dynamic Sizing: Queues in C# automatically resize to accommodate new items. This function enables the handling of various amounts of data without the need for manual resizing or changes.
  • Synchronization and Thread Safety: The Queue class in C# contains synchronization techniques that ensure thread safety when several threads use the queue at the same time. This feature is useful in multithreaded contexts because it prevents data corruption or conflicts during operations.
  • Simple API: The Enqueue function is part of a complete API offered by the Queue class, which makes it simple and clear to add pieces to the queue. This user-friendly interface makes programming and debugging easier.

Due to their First In, First Out (FIFO) characteristic, queues are versatile and can be employed in various algorithms and data structures such as graphs, trees, and more. Consequently, they play a fundamental role in the construction of numerous advanced data structures and algorithms.

Input Required

This code uses input(). Please provide values below: