Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),

MULLANA (AMBALA)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

Queue Data Structure: Types,


Implementation, Applications

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)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

Real Life Queue in Data Structure Example


Let’s consider a queue in data structure example. A line of people is waiting to buy a ticket at a
cinema hall. A new person will join the line from the end, and the person standing at the front
will be the first to get the ticket and leave the line. Similarly in a queue data structure, data added
first will leave the queue first.
Some other applications of the queue in real-life are:

 People on an escalator

 Cashier line in a store

 A car wash line

 One way exits

Types of Queues in Data Structure


There are four different types of queues in data structures:
 Simple Queue

2
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

 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).

 Ordered collection of comparable data kinds.

 Queue structure is FIFO (First in, First Out).

 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.

 The last node is connected to the first node.


 Also known as a Ring Buffer, the nodes are connected end to end.
 Insertion takes place at the front of the queue, and deletion at the end of the queue.
 Circular queue application: Insertion of days in a week.

3
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

4
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

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)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

 Data compression techniques like Huffman code


Below diagram shows how an application use priority queue for the items consumed by the user.

Deque (Double Ended Queue)


In a double-ended queue, insertion and deletion can occur at both the queue's front and rear ends.

6
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

Basic Queue Operations in Queue Data Structure


Below are the basic queue operations in data structure:
Operation Description

enqueue() Process of adding or storing an element to the end of the queue

Process of removing or accessing an element from the front of the


dequeue()
queue

Used to get the element at the front of the queue without


peek()
removing it

initialize() Creates an empty queue

isfull() Checks if the queue is full

isempty() Check if the queue is empty

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)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

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.

Algorithm for Enqueue Operation


procedure enqueuer (data)
if queue is full

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)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

 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.

Queue applications in Data Structure


A queue data structure is generally used in scenarios where the FIFO approach (First In First
Out) has to be implemented. The following are some of the most common queue applications in
data structure:
 Managing requests on a single shared resource such as CPU scheduling and disk
scheduling

9
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

 Handling hardware or real-time systems interrupts


 Handling website traffic
 Routers and switches in networking
 Maintaining the playlist in media players

Implementation 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:

1. #define MAX_SIZE 100


2.

10
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

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)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

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:

1. #include<stdio.h>
2. #include <stdlib.h>
3.

12
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

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)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

41. int main() {


42. enqueue(10);
43. enqueue(20);
44. enqueue(30);
45. printf("%d ", dequeue());
46. printf("%d ", dequeue());
47. printf("%d ", dequeue());
48. printf("%d ", dequeue());
49. return 0;
50. }

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:

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)

Programme: BCA ( 2nd Semester)

Course: BCA-202: Data Structure

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

You might also like