Professional Documents
Culture Documents
-Queues (1)
-Queues (1)
LESSON 3:
QUEUES
OBJECTIVES:
▪ Let the students be aware of the different IDE’s that they could
use in developing a program.
What is a Queue?
The known description of “queue” is a line, usually of people or things waiting
for something. It implements the idea of “first come, first serve”, in which the first
person or thing coming from that line will be prioritized first before turning to the next,
then afterwards they can get out from the line. This idea is often experienced in
different real-world situations such as waiting in line for a movie, waiting at a check-
out line in the grocery store, or ordering from a fast-food restaurant.
Source: lttps://www.vecteezy.com/vector-art/1237890-people-standing-in-line
Figure 1: Sample image of a waiting line for a movie
The most recently added item in the queue must wait at the end of the collection.
The item that has been in the collection the longest is at the front. This ordering
principle is sometimes called “FIFO” (first-in first-out), which is also known as the idea
of “first-come first-served” method. Well-behaved lines, or queues, are very restrictive
that they have only one way in and only one way out. There is no jumping in the middle
and no leaving before you have waited the necessary amount of time to get to the
front. Another real-world example of FIFO can be a single-lane one-way road, where
the vehicle enters first, exits first. More real-world examples can be seen as queues at
the ticket windows and bus-stops.
Just like in stacks, a queue can also be implemented using arrays, linked-lists,
pointers and structures. For the example, we shall implement queues using one-
dimensional array. Initially the head(FRONT) and the tail(REAR) of the queue points
at the first index of the array (starting the index of array from 0). As we add elements
to the queue, the tail keeps on moving ahead, always pointing to the position where
the next element will be inserted, while the head remains at the first index.
The following diagram given below tries to explain queue representation using array.
When we remove an element from Queue, we remove the element from head
position and then move head to the next position. Whenever we move head one
position ahead, after removal of first element, the size on Queue is reduced by one
space each time.
Stacks Queues
Items are added or deleted from Items are added from “Rear” end of the
only one end called “Top” of the queue and are removed from the “front” of
stack. the queue.
The basic operations for the stack The basic operations for a queue are
are “push” and “Pop”. “enqueue” and “dequeue”.
The stack is mostly used to solve Queues are used to solve problems related
recursive problems. to ordered processing.
Basic Operations
To fully utilize or define a queue, there are basic operations that can be used.
These are:
In a queue, we dequeue (or access) the data from the front pointer and
enqueuing (or storing) data in the queue to the rear pointer. There are also few
functions that can be used to make the above-mentioned queue operation efficient.
These are:
▪ peek() − Gets the element at the front of the queue without removing it.
▪ isfull() − Checks if the queue is full.
▪ isempty() − Checks if the queue is empty.
1. peek()
This function helps to see the data at the front of the queue. The algorithm of
peek() function is as follows −
Algorithm:
begin procedure peek
return queue[front]
end procedure
Implementation of peek() function example:
int peek() {
return queue[front];
2. isfull()
Algorithm:
begin procedure full
if rear equals to MAXSIZE
return true
else
return false
endif
end procedure
bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
3. isempty()
Algorithm:
end procedure
If the value of front is less than MIN or 0, it tells that the queue is not yet initialized,
hence empty.
bool isempty() {
if(front < 0 || front > rear)
return true;
else
return false;
}
Applications of Queue
Let us discuss the various applications of the queue data structure below.
▪ The queue data structure is used in various CPU and disk scheduling. Here
we have multiple tasks requiring CPU or disk at the same time. The CPU or
disk time is scheduled for each task using a queue.
▪ The queue can also be used for print spooling wherein the number of print
jobs is placed in a queue.
▪ Handling of interrupts in real-time systems is done by using a queue data
structure. The interrupts are handled in the order they arrive.
▪ Call center phone systems use queues to hold the calls until they are
answered by the service representatives.
In general, we can say that the queue data structure is used whenever we require
the resources or items to be serviced in First in, First Out order.
Enqueue Operation
Dequeue Operation
Next, we will see a detailed illustration of insertion and deletion operations in queue.
Illustration
This is an empty queue and thus we have rear and empty set to -1.
In the next figure, we add element 2 to the queue by moving the rear pointer ahead
by another increment.
In the following figure, we add element 3 and moved the rear pointer.
At this point, the rear pointer is at the 2nd location while the front pointer is at the
0th location.
Next, we delete the element pointed by the front pointer. As the front pointer is at 0,
the element that is deleted is 1.
Thus, the first element entered in the queue i.e. 1 happens to be the first
element removed from the queue. As a result, after the first dequeue, the front
pointer now will be moved ahead to the next location which is 1.
Begin
(See line 27 in the example code)
function Enqueue() to insert elements in queue:
If queue is completely filled up then print “Overflow”.
Otherwise insert element at rear.
Update the value of rear
End
Begin
(See line 44 in the example code)
function Dequeue() to delete elements from queue:
If queue is completely empty then print “Underflow”.
Otherwise insert element from the front.
Update the value of rear.
End
In this example code, it will show how the insertion and deletion of an element in a
queue is applied using C++.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 if(isFull()){
30
31
32 }
33 else{
34 if(front==-1)
35 front=0;
36 cout<<"\nEnter an element: ";
37 cin>>ins_item;
38 cout<<"\n";
39 rear=rear+1;
40 arrayq[rear]=ins_item;
41 }
42 }
43
44 void dequeue(){
45 if(isEmpty()){
46 cout<<"\nQueue is empty!\n";
47 return;
48 }
49 else{
50 cout<<"\nElement removed from queue is:
"<<arrayq[front];
51 cout<<"\n";
52 front++;
53 }
54 }
55
56 void display(){
57 int i;
58 if(isEmpty()){
59 cout<<"\nQueue is empty!\n";
60 return;
61 }
62 else{
63 cout<<"\nElements in queue: ";
64 for(i=front; i<=rear; i++)
65 cout<<arrayq[i]<<" "; 66
cout<<"\n";
67 }
68 }
69
70 void peek(){
71 if(isEmpty()){
72 cout<<"\nQueue is empty!\n";
73 return;
74 }
75 else{
76 cout<< "\nFront element is: " << arrayq[front];}
77 cout<<"\n";
78 }
79
80 int main(){
81 int ch;
82 while(1){
83 cout<<"\n[1] Add\n";
84 cout<<"\n[2] Delete\n";
85 cout<<"\n[3] Display\n";
86 cout<<"\n[4] Display Front\n";
87 cout<<"\n[5] Exit\n";
88 cout<<"\nChoice: ";
89 cin>>ch;
90
91
92
93
94
95
96
97
98
99
100
Output:
Figure 6.2: Shows that the queue is full upon enqueueing an element
Circular Queues
A circular queue or “ring buffer” is a type of queue in which the last position is
connected to the first position, which is represented by the shape of a circle. It is a
linear data structure in which the operations are performed based on FIFO (First In
First Out) principle of queues.
In a normal Queue, we can insert elements until queue becomes full. But once
queue becomes full, we cannot insert the next element even if there is a space in front
of queue.
As you can see in the above image, after a bit of enqueuing and dequeuing, the
size of the queue has been reduced. If you will try to enter an element, it will prompt
that the queue is full although there is still space from the queue. The indexes 0 and
1 can only be used after the queue is reset when all the elements have been
dequeued.
How it works?
Circular Queue works by the process of circular increment. For example, when we try
to increment the pointer and we reach the end of the queue, we start from the
beginning of the queue. With the implementation of Circular Queues, it avoids the
wastage of space in a regular queue implementation using arrays. Circular Queue also
uses the algorithm of enqueue and dequeue.
IT 201: DATA STRUCTURES AND ALGORITHMS 73
UNIT 1: Data Structures
2. dequeue operation
▪ check if the queue is empty
▪ return the value pointed by front
▪ circularly increase the front index by 1
▪ for the last element, reset the values of front and rear to -1
however, the check for full queue has a new additional case:
▪ case 1: front = 0 && rear == size - 1
▪ case 2: front = rear + 1
the second case happens when rear starts from 0 due to circular increment and when
its value is just 1 less than front, the queue is full.
1. enqueue()
begin
(See line 27 in the example code)
if front = 0 and rear = n – 1, or front = rear + 1, then queue is full, and return
otherwise
if front = -1, then front = 0 and rear = 0
else
if rear = n – 1, then, rear = 0, else rear := rear + 1
queue[rear] = key
end
2. dequeue(queue)
begin
(See line 53 in the example code)
if front = -1 then queue is empty, and return
otherwise
item := queue[front]
if front = rear, then front and rear will be -1
else
if front = n – 1, then front := 0 else front := front + 1
end
In this example code, it will show how the insertion and deletion of an element in a
circular queue is applied using C++.
10
11
12
IT 201: DATA STRUCTURES AND ALGORITHMS 76
UNIT 1: Data Structures
13 }
14 else
15 return false; 16 }
17
18 bool isEmpty(){
19 if(front==-1){
20 return true; 21
}
22 else
23 return false; 24 }
25
26
27 void enqueue(){
28 int ins_item;
29 if(isFull()){
30 cout<<"\nQueue Overflow!\n";
31 return;
32 }
33 else{
34 cout<<"\nEnter an element: ";
35 cin>>ins_item;
36 cout<<"\n";
37
38 if(front==-1){
39 front = 0;
40 rear = 0;
41 }
42 else {
43 if(rear == MAX - 1)
44 rear = 0;
45 else
46 rear = rear + 1;
47 }
48 arrayq[rear]=ins_item;
49 }
50 }
51
52
53 void dequeue(){
54 if(isEmpty()){
55 cout<<"\nQueue is empty!\n";
56 return;
57 }
58 else{
59 cout<<"\nElement removed from queue is:
"<<arrayq[front];
60 cout<<"\n";
61 if (front == rear) {
62 front = -1;
63 rear = -1;
64 }
65 else {
66 if(front == MAX - 1)
67 front = 0;
68 else
69 front = front+1;
70 }
71
72 }
73 }
74
75 void display(){
76 int i;
77 if(isEmpty()){
78 cout<<"\nQueue is empty!\n";
79 return;
80 }
81 else{
82 cout<<"\nElements in queue: ";
83 if (front<=rear){
84 for(i=front; i<=rear; i++)
85 cout<<arrayq[i]<<" ";
86 cout<<"\n";
87 }
88 else {
89 while (front <= MAX - 1) {
90 cout<<arrayq[front]<<" ";
91 front++;
92 }
93 front = 0;
94 while (front <= rear) {
95 cout<<arrayq[front]<<" ";
96 front++;
97 }
98 }
99 cout<<endl;
100 }
101
102 }
103
104 void peek(){
105 if(isEmpty()){
106 cout<<"\nQueue is empty!\n";
107 return;
108 }
109 else{
110 cout<< "\nFront element is: " << arrayq[front];}
111 cout<<"\n";
112 }
113
114 void index() //checks the value in the [0] index {
115 if(isEmpty()){
116 cout<<"\nQueue is empty!\n";
117 return;
118 }
119 else{
120 cout<<"\nIndex [0] in array: " << arrayq[0];
121 cout<<"\n";
122 }
123 }
124
125 int main(){
126 int ch;
127 while(1){
128 cout<<"\n[1] Add\n";
129 cout<<"\n[2] Delete\n";
Output:
Figure 10.1: Shows the result when the queue is full. It also
shows the queue elements.
Applications:
Priority Queues
▪ An element with higher priority is processed before other elements with lower
priority.
▪ Elements with the same priority are processed on First-In-First-Out service basis.
Figure 11.3: Shows the element deleted that has higher priority
Other applications of priority queues are found in long term scheduling of jobs
processed in a computer. In practice, short processes are given a priority over long
processes as it improves the average response of the system.
Items are given weight, which allows them to move towards the head of the
queue rather than being on the tail of the queue as would happen in the regular queue.
Inserting an item to the queue doesn’t take a constant time like regular queues,
because we have to apply insertion sort as we insert an item on the basis of their
priority.
Algorithm
Begin
class Priority_Queue has following functions:
function insert() to insert items at priority queue with their priorities:
(See line 27 in the example code)
1) If queue is empty insert data from the left end of the queue.
2) If queue currently have some data then insert the new element at the end of
those elements having priority
same with the new elements and also before all the elements having priority
lesser than the current priority of the new element.
The highest priority element is in the front of the queue. On deletion, the first
element of the queue is being removed from the queue. Element with the same priority
is sorted based on the FIFO.
In this example code, it will show how the insertion and deletion of an element
in a priority queue is applied using C++.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 if(isFull()){
30
31
32
33
34
35 cin>>ins_item;
36
37
38 if(front==-1){
39
40
41
42
43
44
IT 201: DATA STRUCTURES AND ALGORITHMS 85
UNIT 1: Data Structures
45 else
46 rear = rear + 1;
47 }
48 arrayq[rear]=ins_item;
49 }
50 }
51
52
53 void dequeue(){
54 if(isEmpty()){
55 cout<<"\nQueue is empty!\n";
56 return;
57 }
58 else{
59 cout<<"\nElement removed from queue is:
"<<arrayq[front];
60 cout<<"\n";
61 if (front == rear) {
62 front = -1;
63 rear = -1;
64 }
65 else {
66 if(front == MAX - 1)
67 front = 0;
68 else
69 front = front+1;
70 }
71
72 }
73 }
74
75 void display(){
76 int i;
77 if(isEmpty()){
78 cout<<"\nQueue is empty!\n";
79 return;
80 }
81 else{
82 cout<<"\nElements in queue: ";
83 if (front<=rear){
84 for(i=front; i<=rear; i++)
85 cout<<arrayq[i]<<" ";
86 cout<<"\n";
87 }
88 else {
89 while (front <= MAX - 1) {
90 cout<<arrayq[front]<<" ";
91 front++;
92 }
93 front = 0;
94 while (front <= rear) {
95 cout<<arrayq[front]<<" ";
96 front++;
97 }
98 }
99 cout<<endl;
100 }
101
102 }
Output:
Figure 12.1: Shows the elements from the queue and sorted
based on their priority
REFERENCES
Data Structure and Algorithms – Queues. (n.d.). Retrieved August 2020, from
Tutorials Point:
https://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm#:
~:text=Queue%20is%20an%20abstract%20data,first%20will%20be%20acc
essed%20first.
Ahlawat, A. (n.d.) Queue Data Structure | Study Tonight. Retrieved August 2020,
from Study Tonight: https://www.studytonight.com/data-structures/queue-
data-structure
Queue Data Structure on C++ with Illustration. (2020, August 2). Retrieved August
2020, from Software Testing Help:
https://www.softwaretestinghelp.com/queue-in-
cpp/#:~:text=The%20operation%20of%20adding%20%2Finserting,that%20t
he%20queue%20is%20full.
Circular Queue | Set 1 (Introduction and Array Implementation). (2020, August 19)
Retrieved August 2020 from GeeksForGeeks:
https://www.geeksforgeeks.org/circular-queue-set-1-introduction-array-
implementation/
Circular Queue Data Structure. (n.d.). Retrieved August 2020, from Programiz:
https://www.programiz.com/dsa/circular-queue
Mishra, A. (n.d.) Priority Queue in C and C++. Retrieved August 2020 from The
Crazy Programmer: https://www.thecrazyprogrammer.com/2017/06/priority-
queue-c-c.html
Lathiya, A. (2019, October 18) Priority Queue In C++ Example | C++ Priority Queue
Program. Retrieved August 2020 from AppDividend:
https://appdividend.com/2019/10/09/priority-queue-in-cpp-example-cpp-
priority-queue-program/