Professional Documents
Culture Documents
Queue 1
Queue 1
Queue follows the First In First Out (FIFO) rule - the item that goes in first
is the item that comes out first.
In the above image, since 1 was kept in the queue before 2, it is the first to
be removed from the queue as well. It follows the FIFO rule.
In programming terms, putting items in the queue is called enqueue, and
removing items from the queue is called dequeue.
Create(): Creates and initializes new queue that is empty. It does not
require any parameter and returns an empty queue.
Enqueue(item): Add an element to the end (rear) of the queue
Dequeue(): Remove an element from the front of the queue
IsEmpty(): Check if the queue is empty or not. It returns Boolean
value.
IsFull(): Check if the queue is full or not. It returns Boolean value.
Peek(): Get the value of the front of the queue without removing it.
Size(): Returns total number of elements present in the queue.
Working of Queue
Queue operations work as follows:
Algorithm:
Element are added and deleted Element are added and deleted
Operation End from same end. from different end.
Single pointer is used to point top Two pointers are used to point
Pointer of stack. front and rear ends.
It follows LIFO (Last In First Out) It follows FIFO (First In First Out)
Order order. order.
Stack operations are push and Queue operations are enqueue
Operation names pop. and dequeue.
The visualization of stack is of The visualization of stack is of
Visualizations vertical collection. horizontal collection.
Books kept one above other in Queue for booking tickets at
Examples shelf. theatre.
Queue Implementations in C
// Queue implementation in C
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define SIZE 5
void enQueue(int);
void deQueue();
void display();
int main()
{
int ch, x;
while(1)
{
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Enter the element: ");
scanf("%d", &x);
enQueue(x);
break;
case 2:
deQueue();
break;
case 3:
display();
break;
case 4:
exit(0);
}
return 0;
}
void deQueue()
{
if (front == -1)
printf("\nQueue is Empty!!");
else {
printf("\nDeleted : %d", items[front]);
front++;
if (front > rear)
front = rear = -1;
}
}
After REAR reaches the last index, if we can store extra elements in the
empty spaces (0 and 1), we can make use of the empty spaces. This is
implemented by a modified queue called the circular queue.
Applications of Queue
CPU scheduling, Disk Scheduling
Call Center phone systems use Queues to hold people calling them
in order.
Types of Queues
A queue is a useful data structure in programming. It is similar to the ticket
queue outside a cinema hall, where the first person entering the queue is
the first person who gets the ticket.
There are four different types of queues:
Simple Queue
Circular Queue
Priority Queue
Simple Queue
In a simple queue, insertion takes place at the rear and removal occurs at
the front. It strictly follows the FIFO (First in First out) rule.
Circular Queue
Representation
The main advantage of a circular queue over a simple queue is better
memory utilization. If the last position is full and the first position is empty,
we can insert an element in the first position. This action is not possible in a
simple queue.
Priority Queue
A priority queue is a special type of queue in which each element is
associated with a priority and is served according to its priority. If elements
with the same priority occur, they are served according to their order in the
queue.
Here, indexes 0 and 1 can only be used after resetting the queue (deletion
of all elements). This reduces the actual size of the queue.
How Circular Queue Works
Circular Queue works by the process of circular increment i.e. when we try
to increment the pointer and we reach the end of the queue, we start from
the beginning of the queue.
Here, the circular increment is performed by modulo division with the queue
size. That is,
Algorithm:
Write “Overflow”
Goto step 4
[End of If]
2. If FRONT = -1 && REAR = -1 then
Set REAR = 0
Else
Set REAR = (REAR + 1) % MAX
[End of If]
3. Set queue[ REAR ] = val
4. Exit
However, to check for full queue has following case:
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.
2. Dequeue Operation
1. If FRONT = -1
Write “Underflow”
Goto Step 4
[End of If]
3. If FRONT = REAR
Set FRONT = 0
Else
[End of if]
[End of if]
4. Exit
// Adding an element
void enQueue(int element) {
if (isFull())
printf("\n Queue is full!! \n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}
// Removing an element
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
return (-1);
} else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
}
// Q has only one element, so we reset the
// queue after dequeing it. ?
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}
// Display the queue
void display() {
int i;
if (isEmpty())
printf(" \n Empty Queue\n");
else {
printf("\n Front -> %d ", front);
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE) {
printf("%d ", items[i]);
}
printf("%d ", items[i]);
printf("\n Rear -> %d \n", rear);
}
}
int main() {
// Fails because front = -1
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
// Fails to enqueue because front == 0 && rear == SIZE - 1
enQueue(6);
display();
deQueue();
display();
enQueue(7);
display();
Memory management
Traffic Management
Priority Queue
A priority queue is a special type of queue in which each element is
associated with a priority value.
And, elements are served on the basis of their priority. That is, higher
priority elements are served first.
However, if elements with the same priority occur, they are served
according to their order in the queue.
For example, the element with the highest value is considered the highest
priority element. However, in other cases, we can assume the element with
the lowest value as the highest priority element.
5. Simplicity
6. Reasonable support for priority
7. The most important tasks having higher priority are performed first
8. Important tasks do not have to wait for completion of less priority tasks
9. Suitable for applications with varying time and resource requirements
1. Priority queues can be used in OS for the purpose of job scheduling. Here,
the job with higher priority gets processed first.
2. Simulation systems in which priority corresponds to event times.
3. Graph algorithms such as Dijkstra’s shortest path algorithm, Prim’s minimum
spanning tree etc.
4. All queue applications where priority involved.
A linear DS that stores data as a sequence A linear DS in which the last item
of element similar to a real-world queue. connects back to the first item forming a
circle.
Possible to enter new items from rear end Possible to enter and remove elements
and remove the items from the front. from any position.