0% found this document useful (0 votes)
14 views49 pages

CHP 2

Uploaded by

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

CHP 2

Uploaded by

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

Topics to be discussed in queue

⚫ Introduction
⚫ Operations on Queue
⚫ Representation of Queue
⚫ Types of queue
Simple queue
Circular Queue
Priority Queue
Dequeue
⚫ Applications

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Queue Basics
⚫ linear data structure, just like stack
⚫ first element is inserted from one end called
the REAR(also called tail)
⚫ removal of existing element takes place from the other end
called as FRONT(also called head).

⚫ Queue follows FIFO(First in First Out) policy

⚫ Example: movie tickets, if you are first in the queue, then


you will be the first one to get the tickets. Same is the case
with Queue data structure.
⚫ Data inserted first, will leave the queue first.
Prof. Geeta Arwindekar, Datta Meghe
College of Engg
Queue contd….
⚫ A good example of a queue is any queue of consumers
for a resource where the consumer that came first is
served first.
⚫ The difference between stack and queues is in removing.
⚫ In a stack we remove the item the most recently added;
in a queue, we remove the item the least recently added.

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Queue contd….
The practical examples of queues are

⚫ The consumer who comes first to a shop will be served


first.

⚫ CPU task scheduling and disk scheduling.

⚫ Waiting list of tickets in case of bus and train tickets.

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Difference between stack and queue
STACKS QUEUES
Stacks are based on the LIFO Queues are based on the FIFO
principle. principle,
Insertion and deletion in stacks Insertion and deletion in queues
takes place only from one end of takes place from the opposite
the list called the top. ends of the list.
Insert operation is called
Insert operation is called push
enqueue operation.
operation.
Delete operation is called pop Delete operation is called
operation. dequeue operation.
In stacks we maintain only one
In queues we maintain two
pointer to access the list, called
pointers to access the list.
the top
Prof. Geeta Arwindekar, Datta Meghe
College of Engg
Operations in Queue
⚫ process to add an element into queue is
called Enqueue
⚫ process of removal of an element from queue is
called Dequeue.

⚫ 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.

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Implementation of Queue Data Structure
⚫ using an Array

⚫ or Linked List.

The easiest way of implementing a queue is by using an Array.

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Array representation of Queue

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Enqueue
Queues maintain two data pointers, front and rear.
Therefore, its operations are comparatively difficult to
implement than that of stacks.
⚫ Step 1 − Check if the queue is full.
⚫ Step 2 − If the queue is full, produce overflow error and
exit.
⚫ Step 3 − If the queue is not full, increment rear pointer
to point the next empty space.
⚫ Step 4 − Add data element to the queue location, where
the rear is pointing.
⚫ Step 5 − return success.

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Enqueue contd….

⚫ New element D is added from rear

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Algorithm for ENQUEUE operation
procedure enqueue(int data)
if(rear==Max-1)
print “Queue overflow”
else
rear = rear + 1;
queue[rear] = data;
end procedure

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Dequeue
⚫ Step 1 − Check if the queue is empty.
⚫ Step 2 − If the queue is empty, produce underflow error
and exit.
⚫ Step 3 − If the queue is not empty, access the data
where front is pointing.
⚫ Step 4 − Increment front pointer to point to the next
available data element.
⚫ Step 5 − Return success.

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Dequeue contd…

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Algorithm for DEQUEUE operation
procedure dequeue()
if(rear== -1)
print “Queue underflow”
else
int data = queue[front];
front = front + 1;
return data;
end procedure

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
C implementation of Queue Array
#include<stdio.h>
#include<conio.h>
#define MAX 10
void enqueue(int);
void dequeue();
void display();
int queue[MAX], front = -1, rear = -1;

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
C implementation of Queue Array
void main()
{
int value, choice;
clrscr();
while(1)
{
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit"); printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enqueue(value);
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
Prof. Geeta Arwindekar, Datta Meghe
} College of Engg
C implementation of Queue Array
void enqueue(int value)
{
if(rear == MAX-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else
{

rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
}
Prof. Geeta Arwindekar, Datta Meghe
College of Engg
C implementation of Queue Array
void dequeue()
{
if(rear==-1 || front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else
{
front++;
printf("\nDeleted : %d", queue[front]);
}
}

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
C implementation of Queue Array
void display()
{
if(rear == -1)
printf("\nQueue is Empty!!!");
else
{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
}
Prof. Geeta Arwindekar, Datta Meghe
College of Engg
output
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 15
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit Enter your choice : 1
Inset the element in queue : 20
Prof. Geeta Arwindekar, Datta Meghe
1.Insert element to queue
College of Engg
Output contd..
Enter your choice : 1
Inset the element in queue : 30
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Element deleted from queue is : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 3
Queue is : 15 20 30
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 4 Prof. Geeta Arwindekar, Datta Meghe
College of Engg
Queue Types

⚫ There are four types of Queue:

1. Simple Queue
2. Circular Queue
3. Priority Queue
4. Dequeue (Double Ended Queue)

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Simple Queue
•Simple queue defines the simple operation of queue
• insertion occurs at the rear of the list and deletion
occurs at the front of the list.

•Disadvantage:
Cannot insert elements though front elements are
deleted once queue full

30 40 50 60 70 80 90
front rear
Circular Queue
⚫ Overcomes drawback of simple queue

⚫ In a circular queue, the last node is connected to the first


node.

⚫ Circular queue is also called as Ring Buffer.

⚫ Insertion in a circular queue happens at the FRONT and


deletion at the END of the queue.

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Circular Queue
⚫ drawback:? once the queue is completely full, it's not possible to insert
more elements
⚫ When we dequeue any element to remove it from the queue, we are
actually moving the front of the queue forward, thereby reducing the
overall size of the queue. And we cannot insert new elements, because
the rear pointer is still at the end of the queue.

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Circular Queue
Circular queues have a fixed size.
Circular queue follows FIFO principle.
Basic features of Circular Queue
⚫ Initially, the head and the tail pointers will be pointing to the same
location, this would mean that the queue is empty.
⚫ New data is always added to the location pointed by the tail pointer,
and once the data is added, tail pointer is incremented to point to the
next available location.
⚫ Computer controlled Traffic Signal System uses circular queue.

7 0
1
6

2
5
Prof. Geeta Arwindekar, Datta Meghe
4 3 College of Engg
C Program for Circular Queue
#include<stdio.h>
#include<conio.h>
#define MAX 6

int queue[MAX];
int front=0,rear=0,count=0;
C Program for Circular Queue
int main()
{
int ch;
while(1)
{
printf("Enter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3:display();
break;
case 4: exit(0);
default:printf("Invalid option\n");
}
}
}
C Program for Circular Queue
void enqueue()
{
int data;
if(count==MAX)
printf(“Queue Overflow\n”);
else
{
printf(“Enter data\n”);
scanf(“%d”,&data);
queue[rear]=data;
rear=(rear+1)%MAX;
count++;
}
}
C Program for Circular Queue
void dequeue()
{
int data;
if(count==0)
printf(“Queue Underflow\n”);
else
{
data=queue[front];
front=(front+1)%MAX;
count--;
printf(“Element deleted is %d\n”,data);
}
}
C Program for Circular Queue
void display
{
int I,j;
if(count==0)
printf(“Queue Underflow\n”);
else
{
printf(“Elements in queue are\n”);
j=Count;
for(i=front;j!=0;j--)
{
printf(“%d “,queue[i]);
i=(i+1)%MAX;
}
}
}
Circular Queue output
Priority Queue

• contains data items which have some preset


priority.

• While deleting an element from a priority queue,


the data item with the highest priority is
removed first.

• insertion is performed in the order of arrival


Priority Queue contd…
Two types
Ascending priority queue
Descending priority queue
In Ascending priority queue, elements are inserted
with smallest element at beginning and largest at end
And opposite in descending priority queue
Can be implemented in two ways
First insert all elements and sort it
As and when elements appear, insert it
according to priority
Example of Priority Queue

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Descending Priority Queue
Enqueue(3) max=6 count=1
3
Enqueue(5) max=6 count=2
5 3
Enqueue(9) max=6 count=3
9 5 3
Enqueue(1) max=6 count=4
9 5 3 1
Enqueue(12) max=6 count=5
12 9 5 3 1
Descending Priority Queue
Enqueue(15) count=6
15 12 9 5 3 1
Enqueue(16) Queue full
Dequeue() element 15 is deleted count=5
12 9 5 3 1
Enqueue(16) count=6
16 15 12 9 5 3
Dequeue() element 16 is deleted count=5

15 12 9 5 3
C code for Descending Priority Queue
void enqueue()
{
If(count==MAX)
printf(“Queue is full\n”);
else if(count==0)
{
queue[count]=data;
count++;
}
else
{
for(i=count-1;i>=0;i--)
{
If(data>queue[i])
{
queue[i+1]=queue[i];
}
else
{
break;
}
}queue[i+1]=data;
count++;
}
}
C code for Descending Priority Queue

void dequeue()
{
return(queue[--count];
}
Dequeue
⚫ Double Ended Queue
⚫ insertion and deletion operations can
be done at both FRONT and END of the
queue.

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Dequeue (Double Ended Queue)

insert and delete operation can be occur at both


ends that is front and rear of the queue.

Provides four operations


•Enqueue_rear
•Enqueue_front
•Dequeue_front
•Dequeue_rear
Enqueue at rear end
void enqueue_rear()
{
int num;
if(rear<front)
{
printf("\n Queue is Overflow");
return;
}
else
{
printf("\n Enter Item to insert : ");
scanf("%d",&num);
q[rear]=num;
rear--;
}
}
Enqueue at font end
void enqueue_front()
{
int num;
if(front>rear)
{
printf("\n Cannot add item at front end");
return;
}
else
{
printf("\n Enter item to insert:");
scanf("%d",&num);
q[front]=num;
Front++;
}
}
Dequeue from front end
void dequeue_front()
{
int num;
if(front1>rear1)
{
printf("\n Queue is Underflow\n");
return;
}
else
{
num=q[front1];
printf("\n Deleted item is %d\n",num);
front1++;
}
}
Dequeue from rear end
void dequeue_rear()
{
int num;
if(rear1<front1)
{
printf("\n Cannot delete item at rear end\n");
return;
}
else
{
num=q[rear1];
rear1--;
printf("\n Deleted item is %d\n",num);
}
}
}
Types of Dequeue
Double Ended Queue can be represented in
TWO ways
•Input Restricted Double Ended Queue
•Output Restricted Double Ended Queue

An input-restricted deque
deletion can be made from both ends, but
insertion can be made at one end only.

An output-restricted deque
insertion can be made at both ends, but deletion
can be made from one end only.
Types of Dequeue

Prof. Geeta Arwindekar, Datta Meghe


College of Engg
Queue Applications
⚫ Scheduling Algorithms
⚫ Round robin technique algorithm
⚫ Printer server routine
⚫ Multiprogramming Platform
⚫ Various application software is also based on queue data
structures
⚫ Keyboard buffer
⚫ OS resource sharing eg CPU scheduling, Disk scheduling

Prof. Geeta Arwindekar, Datta Meghe


College of Engg

You might also like