Queue

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

ICS121 – Data Structures I

Module 1 – Fundamentals of Data Structures:


Queue, Circular Queue

Dr Athira B
Assistant Professor
Department of Computer Science and Engineering
IIIT Kottayam
Queue:

• Queues are lists.

• With a queue, insertion is done at one end, whereas deletion is


performed at the other end.

• The basic operation on a queue is Enqueue, which inserts an


element at the end of the list(called the rear), and Dequeue, which
deletes the element at the start of the list(Known as the front)
Time Complexity:
Queue Operations:
• Enqueue()
• Dequeue()
• IsEmpty()
• MakeEmpty();

Types of Queue:
• Linear Queue
• Circular Queue
• Priority Queue
• Deque
Array Implementation of Queue Operations:
Enqueue Algorithm:

1. START begin
2. Check if the queue is full. if Rear = max - 1
3. If the queue is full, produce overflow error write “overflow”
and exit. else
4. If the queue is not full, increment rear set Rear = Rear + 1
pointer to point the next empty space. set Queue[Rear] = num
5. Add data element to the queue location, end
where the rear is pointing.
6. return success.
7. END
Dequeue Algorithm

begin
1. START if Front = -1 or Front > Rear
2. Check if the queue is empty. write “underflow”
3. If the queue is empty, produce underflow error and
else
exit.
4. If the queue is not empty, access the data where front set val = Queue[Front]
is pointing. set Front = Front + 1
5. Increment front pointer to point to the next available
data element. end if
6. Return success.
end
7. END
Routine to test Queue is empty

1. START
2. If the count of queue elements equals
zero, return true
3. Otherwise, return false
4. END

Routine to test Queue is full 1. START


2. If the count of queue elements equals
int isfull(Queue Q) the queue size, return true
{ 3. Otherwise, return false
Return Q -> Size == MAX; 4. END
}
Display/tracing:

Algorithm:
begin
if(front==-1 and Rear == -1)
write "Empty queue";
else
for(i=Front;i<=Rear;i++)
Print( Queue[i]);
end if
end
#include <stdio.h>
void enqueue (int data){
#include <string.h>
if(!isFull()) {
#include <stdlib.h>
if(rear == MAX-1) {
#include <stdbool.h>
rear = -1; }
#define MAX 6
intArray[++rear] = data;
int intArray[MAX];
itemCount++; }
int front = 0;
}
int rear = -1;
int itemCount = 0;
int main(){
enqueue(3);
bool isFull(){
enqueue(5);
return itemCount == MAX;
enqueue(9);
}
enqueue(1);
enqueue(12);
bool isEmpty(){
enqueue(15);
return itemCount == 0;
printf("Queue: ");
}
while(!isEmpty()) {
int n = dequeue();
int dequeue (){
printf("%d ",n); }
int data = intArray[front++];
}
if(front == MAX) {
front = 0; }
itemCount--;
return data; }
Array implementation of Circular Queue
Operations – EnQueue()
front=rear=-1
enqueue(int element)
{
if(front==-1 && rear==-1)//empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) //full write
"Queue is overflow";
else
{
rear=(rear+1)%max;
queue[rear]=element;
}
}
DeQueue Operation:
dequeue()
{
if((front==-1) && (rear==-1))
//empty
write “Queue is underflow";
else else
Write queue[front]; //printing dequeued
element
front=(front+1)%max;
}
Tracing:
int i=front;
if(front==-1 && rear==-1)
Write "Queue is empty";
else
{
while(i<=rear)
{
write queue[i];
i=(i+1)%max;
}
}
Priority Queue:
• Queues are a standard mechanism for ordering tasks on a first-come, first-served basis.
• However, some tasks may be more important or timely than others (higher priority).
• Real-time Applications: To-do remainder list and Hospital Emergency queue.
• It supports only those elements that are comparable. Hence, a priority queue in the data
structure arranges the elements in either ascending or descending order.
Priority queue through an example.
• Example: 2,4,5,10,12,18

• poll(): remove the highest priority element from the priority queue. The ‘2’ element has the highest
priority, so it will be removed from the priority queue.
Ans: 4,5,10,12,18
• add(3): insert ‘3’ element in a priority queue. As 3 is the smallest element among all the numbers so it
will obtain the highest priority.
Ans:3,4,5,10,12,18
• poll(): It will remove ‘3’ element from the priority queue as it has the highest priority
queue. Ans:4,5,10,12,18
• add(6): It will insert 6 element after 5 as 6 is larger than 5 and lesser than 10, so it will
obtain the third
highest priority in priority queue.
Ans:4,5,6,10,12,18
Types:
Ascending order priority queue: In ascending order priority queue, a lower priority number is given as a
higher priority in a priority order.

Descending order priority queue: In descending order priority queue, a higher priority number is given as
a higher priority in a priority order.
Characteristics of a Priority Queue
• A queue is acknowledged as a priority queue if it has the following characteristics:
• Each item has some priority associated with it.
• An item with the highest priority is moved at the front and deleted first.
• If two elements share the same priority value, then the priority queue follows the first-in-
first-out principle for dequeue operation.

Insert 7, 2, 45, 32, and 12 in a priority queue

2, 7, 12, 32, 45
Applications:
• It is used in breadth search operation in graphs.
• Job scheduler operations of OS like a print buffer queue, keyboard buffer
queue to store the keys pressed by users
• Job scheduling, CPU scheduling, Disk Scheduling
• Priority queues are used in file downloading operations in a browser
• Data transfer between peripheral devices and CPU.
• Interrupts generated by the user applications for CPU
• Calls handled by the customers in BPO

You might also like