Queue Data Structure
Queue Data Structure
MULLANA (AMBALA)
A queue is a linear data structure that stores the elements sequentially. It uses the FIFO (First
In First Out) approach for accessing elements. Queues are typically used to manage threads in
multithreading and implementing priority queuing systems. Let’s learn about different types of
queue data structures, basic operations performed on them, implementation, and queue
applications.
A queue is an important data structure in programming. A queue follows the FIFO (First In
First Out) method and is open at both of its ends. Data insertion is done at one end, the rear end
or the tail of the queue, while deletion is done at the other end, called the front end or the head of
the queue.
1
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
People on an escalator
2
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Circular Queue
Priority Queue
Double-Ended Queue (Deque)
Simple Queue
Simple Queue is a linear data structure that follows the First-In-First-Out (FIFO) principle,
where elements are added to the rear (back) and removed from the front (head).
When a new element is added, all elements added before the new element must be
deleted in order to remove the new element.
Circular Queue
A circular queue is a special case of a simple queue in which the last member is linked to the
first. As a result, a circle-like structure is formed.
3
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
4
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Now let’s take a look at the priority queue in the data structure.
Priority Queue
In a priority queue, the nodes will have some predefined priority in the priority queue. The node
with the least priority will be the first to be removed from the queue. Insertion takes place in the
order of arrival of the nodes.
Some of the applications of priority queue:
Dijkstra’s shortest path algorithm
Prim’s algorithm
5
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
6
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Now, let us understand in detail the two primary operations associated with Queue data structure
– enqueue and dequeue.
7
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Enqueue Operation
Below are the steps to enqueue (insert) data into a queue
1. Check whether the queue is full or not.
2. If the queue is full – print the overflow error and exit the program.
3. If the queue is not full – increment the rear pointer to point to the next empty space.
4. Else add the element in the position pointed by Rear.
5. Return success.
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
Dequeue Operation
Below are the steps to perform the dequeue operation
Check whether the queue is full or not.
8
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
If the queue is empty – print the underflow error and exit the program.
If the queue is not empty – access the data where the front is pointing.
Else increment the front pointer to point to the next available data element.
Return success.
Algorithm for Dequeue Operation
procedure dequeue
if queue is empty
return underflow
end if data = queue[front]front ← front + 1return trueend procedure
Implementation of Queue
A queue can be implemented in two ways:
Sequential allocation: It can be implemented using an array. A queue implemented using
an array can organize only a limited number of elements.
Linked list allocation: It can be implemented using a linked list. A queue implemented
using a linked list can organize unlimited elements.
Now, let’s move on to the application of queue.
9
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
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.
C Programming Language:
10
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
3. int queue[MAX_SIZE];
4. int front = -1;
5. int rear = -1;
6.
7. void enqueue(int element) {
8. if (rear == MAX_SIZE - 1) {
9. printf("Queue is full");
10. return;
11. }
12. if (front == -1) {
13. front = 0;
14. }
15. rear++;
16. queue[rear] = element;
17. }
18.
19. int dequeue() {
20. if (front == -1 || front > rear) {
21. printf("Queue is empty");
22. return -1;
23. }
24. int element = queue[front];
25. front++;
26. return element;
27. }
28.
29. int main() {
30. enqueue(10);
31. enqueue(20);
32. enqueue(30);
33. printf("%d ", dequeue());
34. printf("%d ", dequeue());
35. printf("%d ", dequeue());
36. printf("%d ", dequeue());
37. return 0;
38. }
11
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Output:
10 20 30 Queue is empty-1
Explanation:
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.
C Programming Language:
1. #include<stdio.h>
2. #include <stdlib.h>
3.
12
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
4. struct Node {
5. int data;
6. struct Node* next;
7. };
8.
9. struct Node* front = NULL;
10. struct Node* rear = NULL;
11.
12. void enqueue(int element) {
13. struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
14. new_node->data = element;
15. new_node->next = NULL;
16. if (front == NULL && rear == NULL) {
17. front = rear = new_node;
18. return;
19. }
20. rear->next = new_node;
21. rear = new_node;
22. }
23.
24. int dequeue() {
25. if (front == NULL) {
26. printf("Queue is empty");
27. return -1;
28. }
29. struct Node* temp = front;
30. int element = temp->data;
31. if (front == rear) {
32. front = rear = NULL;
33. }
34. else {
35. front = front->next;
36. }
37. free(temp);
38. return element;
39. }
40.
13
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Output:
10 20 30 Queue is empty-1
Explanation:
Advantages:
o 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.
o Because queue operations have an O(1) time complexity, they can be executed fast even
on enormous queues.
o Queues are particularly flexible since they may be simply implemented using an array or
a linked list.
Disadvantages:
14
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
o A queue, unlike a stack, cannot be constructed with a single pointer, making queue
implementation slightly more involved.
o 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.
o 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.
15