0% found this document useful (0 votes)
13 views14 pages

Lecture 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views14 pages

Lecture 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Lecture 5: Queue

Md. Nazmul Abdal


Lecturer
Department of CSE
University of Liberal Arts Bangladesh (ULAB)
Introduction

⚫ Queue follows the First In First Out (FIFO) rule - the item that
goes in first is the item that comes out first.

⚫ In programming terms, putting items in the queue is called


enqueue, and removing items from the queue is called dequeue.
Basic Operations

⚫ Enqueue: Add an element to the end of the queue

⚫ Dequeue: Remove an element from the front of the queue

⚫ IsEmpty: Check if the queue is empty

⚫ IsFull: Check if the queue is full

⚫ Peek: Get the value of the front of the queue without removing it
Working Procedure

⚫ Two pointers FRONT and REAR


⚫ FRONT track the first element of the queue
⚫ REAR track the last element of the queue
⚫ Initially, set value of FRONT and REAR to -1

Enqueue Operation
⚫ Check if the queue is full
⚫ For the first element, set the value of FRONT to 0
⚫ Increase the REAR index by 1
⚫ Add the new element in the position pointed to by REAR

Dequeue Operation
⚫ Check if the queue is empty
⚫ Return the value pointed by FRONT
⚫ Increase the FRONT index by 1
⚫ For the last element, reset the values of FRONT and REAR to -1
Working Procedure
Example

#include <stdio.h> // 6th element can't be added to


#define SIZE 5 because the queue is full
void enQueue(int); enQueue(6);
void deQueue();
void display(); display();
int items[SIZE], front = -1, rear = -1;
int main() { //deQueue removes element
entered first i.e. 1
//deQueue is not possible on empty
queue deQueue();
deQueue();
//enQueue 5 elements //Now we have just 4 elements
enQueue(1); display();
enQueue(2);
enQueue(3); return 0;
enQueue(4); }
enQueue(5);
Example

void enQueue(int value) {


if (rear == SIZE - 1)
printf("\nQueue is Full!!"); // Function to print the queue
else { void display() {
if (front == -1) if (rear == -1)
front = 0; printf("\nQueue is Empty!!!");
rear++; else {
items[rear] = value; int i;
printf("\nInserted -> %d", value); printf("\nQueue elements are:\n");
} for (i = front; i <= rear; i++)
} printf("%d ", items[i]);
void deQueue() { }
if (front == -1) printf("\n");
printf("\nQueue is Empty!!"); }
else {
printf("\nDeleted : %d", items[front]);
front++;
if (front > rear)
front = rear = -1;
}
}
Applications

⚫ CPU scheduling, Disk Scheduling

⚫ When data is transferred asynchronously between two


processes.The queue is used for synchronization. For example: IO
Buffers, pipes, file IO, etc

⚫ Handling of interrupts in real-time systems.

⚫ Call Center phone systems use Queues to hold people calling them
in order.
Types of Queue

⚫ Simple Queue

⚫ Circular Queue

⚫ Priority Queue

⚫ Double Ended 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

⚫ In a circular queue, the last element points to the first element


making a circular link.
⚫ 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.
⚫ Insertion occurs based on the arrival of the values and removal
occurs based on priority.
Double Ended Queue

⚫ In a double ended queue, insertion and removal of elements can be


performed from either from the front or rear.
⚫ Thus, it does not follow the FIFO (First In First Out) rule.
Thank You

You might also like