Queue.Enqueue() Method in C#

The Queue.Enqueue() method is used to add an item to the end of the Queue. A queue is a data structure that operates on the First-In-First-Out (FIFO) principle, which states that the element inserted first is the first to be withdrawn. The Enqueue() function belongs to the Queue<T> class, where T denotes the type of entries in the queue. It comes under the System.Collections package. This approach is an O(1) operation if the Count is smaller than the capacity of the inner array. If the internal array must be reallocated to accommodate the additional element, this approach becomes an O(n) operation, in which n is Count.

When you call the Enqueue() function, you pass an argument corresponding to the element you would like to add to the queue. After that, this element is added to the end of the queue, so it is the last element in the line. If the list of elements is empty, the new element becomes the queue's single element. The time complexity of the Enqueue() function is O(1), which indicates that it has a constant time complexity irrespective of the size of the queue. New components may be added to a queue without requiring the old elements to be traversed or rearranged.

Syntax:

It has the following syntax:

Here, the ob indicates the object that is to be added.

Example:

Let us take an example to illustrate the Queue.Enqueue() method in C#.

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.

Because of their FIFO nature, queues are adaptable and may be utilized in several algorithms and data structures (including graphs, trees, and others). As a result, they are a key building component for many other complicated data structures and algorithms.






Latest Courses