Javatpoint Logo
Javatpoint Logo

Queue in C

In computer science, a queue is a linear data structure where the components are put into one end and removed from the other end according to the "first-in, first-out" (FIFO) principle. This data structure can be utilized for controlling an action sequence or storing data. C is a computer language with queue capability incorporated in the form of arrays or linked lists.

Characteristics:

  • A queue is a type of linear data structure that can be constructed with an array or a linked list.
  • Elements are relocated to the back of the queue while being removed from the front.
  • Enqueue (add an element to the back) and dequeue (remove an element from the front) are two queue operations.
  • When elements are added and removed often, a queue might be built as a circular queue to prevent wasting memory.

Using Array:

To implement a queue in C using an array, first define the queue's maximum size and declare an array of that size. The front and back integers were respectively set to 1. The front variable represents the front element of the queue, and the back variable represents the back element.

Before pushing the new element to the final position of the queue, we need to increase the back variable by 1. The queue is now full and no other additional elements can be added when the back position is reaching the queue's maximum capacity. We add an element to the front of the queue and increase the front variable by one only to remove an element from the queue. If the front and rear positions are equal and no more components can be deleted, hence we can say that the queue is empty.

Below is an instance of a queue written in C that makes use of an array:

C Programming Language:

The output of the code will be:

Output:

10 20 30 Queue is empty-1

Explanation:

  1. First, we enqueue three elements 10, 20 and 30 into the queue.
  2. Then, we dequeue and print the front element of the queue, which is 10.
  3. Next, we dequeue and print the front element of the queue again, which is 20.
  4. Then, we dequeue and print the front element of the queue again, which is 30.
  5. Finally, we make a dequeue from an empty queue that outputs "Queue is empty" and returns -1.

Using Linked List:

Another alternate approach to constructing a queue in the programming language C is using a linked list. Each of the nodes in the queue is expressed using this method by a node which contains the element value and a pointer to the following node in the queue. In order to keep track of the first and last nodes in the queue, respectively, front and rear pointers are used.

We establish a new node with the element value and set its next pointer to NULL to add an element to the queue. To the new node, we set the front and rear pointers if the queue is empty. If not, we update the rear pointer and set the old rear node's next pointer to point to the new node.

When deleting a node from a queue, the preceding node is deleted first, then the front pointer is updated to the subsequent node in the queue, and finally the memory that the removed node was occupying is released. If the front pointer is NULL after removal, the queue is empty.

Here's an example of a queue implemented in C using a linked list:

C Programming Language:

The output of the code will be:

Output:

10 20 30 Queue is empty-1

Explanation:

  1. First, we enqueue three elements 10, 20 and 30 into the queue.
  2. Then, we dequeue and print the front element of the queue, which is 10.
  3. Next, we dequeue and print the front element of the queue again, which is 20.
  4. Then, we dequeue and print the front element of the queue again, which is 30.
  5. Finally, we try to dequeue from the empty queue, which prints the message "Queue is empty" and returns -1.

Advantages:

  • Queues are particularly useful for implementing algorithms that require elements to be processed in a precise sequence, such as breadth-first search and task scheduling.
  • Because queue operations have an O(1) time complexity, they can be executed fast even on enormous queues.
  • Queues are particularly flexible since they may be simply implemented using an array or a linked list.

Disadvantages:

  • A queue, unlike a stack, cannot be constructed with a single pointer, making queue implementation slightly more involved.
  • If the queue is constructed as an array, it might soon fill up if too many elements are added, resulting in performance concerns or possibly a crash.
  • When utilising a linked list to implement the queue, the memory overhead of each node can be significant, especially for small elements.

Conclusion:

Applications that use queues, a crucial data structure, include operating systems, networking, and games, to name just a few. They are ideal for algorithms that must handle elements in a particular order since they are simple to create using an array or linked list. However, queues have disadvantages that must be considered when selecting a data structure for a particular application, such as memory consumption and implementation complexity.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA