Unit3 Queues
Unit3 Queues
Data Structures
0 1 2 3 4 5 6 7 8 9
12 9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
Array Representation of Queues
• Now, front = 0 and rear = 6. Every time a new element has to be
added, we will repeat the same procedure.
• Now, if we want to delete an element from the queue, then the value
of front will be incremented. Deletions are done from only this
end of the queue.
9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
[END OF IF]
Step 2: Exit
Write a program to implement a Linear Queue.
#include <stdio.h> switch(option)
#include <conio.h> { case 1:
#define MAX 10 // change length of array insert(); break;
int queue[MAX]; case 2:
int front = -1, rear = -1; val = delete_element();
void insert(void); if (val != -1)
int delete_element(void); printf(“\n The number deleted is : %d”, val);
int peek(void); break;
void display(void); case 3:
int main() val = peek();
{ int option, val; if (val != -1)
do{ printf(“\n The first value in queue is : %d”, val);
printf(“\n\n ***** MAIN MENU *****”); break;
printf(“\n 1. Insert an element”); case 4:
printf(“\n 2. Delete an element”); display(); break;
printf(“\n 3. Peek”); }
printf(“\n 4. Display the queue”); }while(option != 5);
printf(“\n 5. EXIT”); getch();
printf(“\n Enter your option : “); return 0;
scanf(“%d”, &option); }
void insert()
{ int num;
printf(“\n Enter the no. to be inserted in the queue : “);
scanf(“%d”, &num); int peek()
if(rear == MAX-1) { if(front==-1 || front>rear)
printf(“\n OVERFLOW”); { printf(“\n QUEUE IS EMPTY”);
else if(front == -1 && rear == -1) return -1;
front = rear = 0; }
else else
rear++; return queue[front];
queue[rear] = num; }
}
int delete_element() void display()
{ int val; { int i;
if(front == -1 || front>rear) printf(“\n”);
{ printf(“\n UNDERFLOW”); if(front == -1 || front > rear)
return -1; } printf(“\n QUEUE IS EMPTY”);
else else
{ val = queue[front]; { for(i = front;i <= rear;i++)
front++; printf(“\t %d”, queue[i]);
if(front > rear) }
front = rear = -1; }
return val;}
}
Linked Representation of Queues
• In a Linked Queue, every element has two parts: one that stores data
and the other that stores the address of the next element.
• The START pointer of the linked list is used as FRONT.
• We will also use another pointer called REAR which will store the
address of the last element in the queue.
• All insertions will be done at the rear end and all the deletions will
be done at the front end.
• If FRONT = REAR = NULL, then it indicates that the queue is
empty.
1 7 3 4 2 6 5 X
FRONT REAR
Inserting an Element in a Linked Queue
Step 1: Allocate memory for the new node and name it as nn
Step 2: SET nn->DATA = VAL
Step 3: IF FRONT = NULL, then
SET FRONT = REAR = nn
SET FRONT->NEXT = REAR->NEXT = NULL
ELSE
SET REAR->NEXT = nn
SET REAR = nn
SET REAR->NEXT = NULL
[END OF IF]
Step 4: END
90 49 7 18 14 36 45 21 99 72
front=0 1 2 3 4 5 6 7 8 rear = 9
▪ If rear != MAX – 1, then the rear will be incremented and value will
be inserted
90 49 7 18 14 36 45 21 99
front=0 1 2 3 4 5 6 7 rear= 8 9
front=1 2 3 4 5 6 7 8 rear= 9
Algorithm to Insert an Element in a
Circular Queue
0 1 2 3 4 5 6 7 8 9
▪ If the queue is not empty and front = rear, then it means now the
queue has become empty and so front and rear are set to -1.
0 1 2 3 4 5 6 7 8 front=rear= 9
▪ If the queue is not empty and if front = MAX -1, then front is set to
0.
72 63 9 18 27 39 81
0 1 2 3 4 rear= 5 6 7 8 front= 9
Algorithm to Delete an Element from a Circular Queue
Step 4: EXIT
Deques
• A deque (Double Ended Queue) is a list in which elements can be
inserted or deleted at either end.
• It is also known as a head-tail linked list because elements can be
added to or removed from the front (head) or back (tail).
• A deque can be implemented either using a circular array or a
circular doubly linked list.
• In a deque, two pointers are maintained, LEFT and RIGHT which
point to either end of the deque.
• The elements in a deque stretch from LEFT end to the RIGHT and
since it is circular, Dequeue[N-1] is followed by Dequeue[0].
Deques
• There are two variants of a double-ended queue:
➢ Input restricted deque: In this dequeue insertions can be done only
at one of the ends while deletions can be done from both the ends.
➢ Output restricted deque: In this dequeue deletions can be done only
at one of the ends while insertions can be done on both the ends.
29 37 45 54 63
0 1 2 LEFT = 3 4 5 6 RIGHT = 7 8 9
63 27 18
42
RIGHT = 0 1 2 3 4 5 6 LEFT = 7 8 9
Priority Queues
• A priority queue is a queue in which each element is assigned a
priority.
• The priority of elements is used to determine the order in which
these elements will be processed.
• The general rule of processing elements of a priority queue can be
given as:
➢ An element with higher priority is processed before an element
with lower priority
➢ Two elements with same priority are processed on a first come
first served (FCFS) basis
• Priority queues are widely used in operating systems to execute the
highest priority process first.
• In computer’s memory priority queues can be represented using
arrays or linked lists.
Priority Queues
Two types:
1. Ascending Priority queue: The element which is having higher priority is served first
2. Descending Priority queue: The element which is having least priority is served first
Linked Representation of Priority
Queues
• When a priority queue is implemented using a linked list, then
every node of the list contains three parts:
(i) the information or data part
(ii) the priority number of the element
(iii) and address of the next element.
• If we are using a sorted linked list, then element having higher
priority will precede the element with lower priority.
A 1 B 2 C 3 D 5 E 6 X
A 1 B 2 C 3 F 4 D 5 E 6 X
A 1 B 2 F 2 C 3 D 5 E 6 X
Array Representation of Priority
Queues
• When arrays are used to implement a priority queue, then a
separate queue for each priority number is maintained.
• Each of these queues will be implemented using circular arrays
or circular queues. Every individual queue will have its own
FRONT and REAR pointers.
• We can use a two-dimensional array for this purpose where each
queue will be allocated same amount of space.
• Given the front and rear values of each queue, a two dimensional
matrix can be formed.
For example, if we have to insert an element R with priority number 3,then the priority queue will be
given as shown in Fig.
Multiple Queues
• When implementing a queue using an array, the size of the array must
be known in advance.
• If the queue is allocated less space, then frequent OVERFLOW
conditions will be encountered.
• To deal with this problem, the code will have to be modified to
reallocate more space for the array, but this results in sheer wastage
of memory. Thus, there lies a tradeoff between the frequency of
overflows and the space allocated.
• A better solution to deal with this problem is to have multiple queues
or to have more than one queue in the same array.
• One important point to note is that while queue A will grow from left
to right, the queue B on the same time will grow from right to left.
0 1 2 3 4 ………………………………. n-4 n-3 n-2 n-1
Queue A Queue B
Applications of Queues
• Queues are widely used as waiting lists for a single shared resource
like printer, disk, CPU.
• Queues are used to transfer data asynchronously e.g., pipes, file IO,
sockets.
• Queues are used as buffers on MP3 players and portable CD players,
iPod playlist.
• Queues are used in Playlist for jukebox to add songs to the end, play
from the front of the list.
• Queues are used in OS for handling interrupts. When programming a
real-time system that can be interrupted, for ex, by a mouse click, it is
necessary to process the interrupts immediately before proceeding
with the current job. If the interrupts have to be handled in the order
of arrival, then a FIFO queue is the appropriate data structure