0% found this document useful (0 votes)
9 views38 pages

Queue

Data structure notes Topic. Queue

Uploaded by

55- Sheldon
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)
9 views38 pages

Queue

Data structure notes Topic. Queue

Uploaded by

55- Sheldon
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/ 38

Ch 2.

Queue
Queue
• Queue is an abstract data structure, somewhat similar to Stack.
• In contrast to Stack, queue is opened at both end.
• One end is always used to insert data (enqueue)
• and the other is used to remove data (dequeue).
• Queue follows First-In-First-Out methodology, i.e., the data item stored first
will be accessed first.
• A real world example of queue can be a single-lane one-way road,where
the vehicle enters first, exits first.
• More real-world example can be seen as queues at ticket windows &
bus-stops.
Queue Representation
• As we now understand that in queue, we access both ends for
different reasons, a diagram given below tries to explain queue
representation as data structure:
Basic Operations

• Queue operations may involve initializing the queue, utilizing it and


then completing erasing it from memory. Here we consider basic
operations associated with queues :
• enqueue() − add (store) an item to the queue.
• dequeue() − remove (access) an item from the queue.
Basic Operations.....

• Few more functions are required to make above mentioned queue


operation efficient. These are −
• peek() − get the element at front of the queue without removing it.
• isfull() − checks if queue is full.
• isempty() − checks if queue is empty.
• In queue, we always dequeue (or access) data, pointed by front pointer
• and while enqueing (or storing) data in queue we take help of rear
pointer.
Implementation of Queues using arrays
Algorithm for Enqueue operation
1: IF Rear = Max-1
Write OVERFLOW
Goto 4
2: IF Front = -1 and Rear = -1
(Check if queue is empty)
SET Front = Rear = 0
ELSE SET Rear = Rear+1
3: SET QUEUE[Rear] = Value
4: EXIT
Algorithm for Dequeue operation
1: IF Front = -1 OR Front > Rear
Write UNDERFLOW
ELSE SET Val = QUEUE[Front]
SET Front = Front+1
2: EXIT
Implementation of Queue using array
void insert() void delete()
{ {
int num; if(front==-1 && rear==-1)
if(front == 0 && rear == max-1) { {
printf("\nQueue is full"); printf("Queue is Empty");
} }
else else
{ {
printf("\nEnter the data to be inserted : "); scanf("%d",&num); printf("\nThe deleted element is %d", queue[front]);
if(front==-1 && rear==-1) if(front == rear)
{ {
front = rear = 0;
front = rear = -1;
}
queue[rear] = num;
else if(front == max-1)
}
{
else if(front!=0 && rear == max-1)
front = 0;
{ rear = 0;
}
queue[rear] = num; } else
else {
{ front++;
rear++; }
queue[rear] = num; }
} }
}}
void peek() If(front<rear)
{ {
if(front== -1 && rear== -1) for(i=front;i<rear;i++)
{ {
printf("Queue is Empty"); Printf(“%d\t”,queue[i]);
}
}
}
Else
{
printf(" \nThe first element in queue is
%d",queue[front]);
}
}
void display()
{ int i;
if(front== -1 && rear== -1)
{
printf(" \nQueue is Empty"); }
else
{
printf(" \nThe elements in the queue are : ");
Take an empty Queue of size 5. Apply following operations on it.
• Enqueue(A), Enqueue(B), Enqueue(C), Enqueue(D), dequeue,
Enqueue(E)
• What is the position of front and rear pointer?
Types of queue
• Circular Queue
• A Circular queue is a linear data structure where the first index comes right
after the last index assuming indices are attached in a circular manner.
• Priority Queue
• A priority queue is a queue in which every element is assigned a priority.
• Double Ended Queue
• Deque is special type of queue in which the elements can be inserted or
deleted at both the ends.
Circular Queue
• Drawback of Linear Queue/Need of Circular Queue:
Solutions
• Solution 1: Shift the elements to the left.
• Solution 2: Use Circular Queue, where the first index comes right
after last index.
Inserting an element in Circular Queue
Algorithm to insert an element in Circular Queue
Deleting an element from Circular Queue
Deleting an element from Circular Queue
Program for Circular Queue using Array
Program for Circular Queue using Array..
Program for Circular Queue using Array..
Priority Queue
• A priority queue is special type of Queue data structure in which each element has some priority associated with
it. Based on the priority of the element, the elements are arranged in a priority queue. If the elements occur with
the same priority, then they are served according to the FIFO principle.

• The highest priority element comes first and the elements of the same priority are arranged based on FIFO
structure.

• For example, suppose we have some values like 1, 3, 4, 8, 14, 22 inserted in a priority queue with an ordering
imposed on the values is from least to the greatest. Therefore, the 1 number would be having the highest priority
while 22 will be having the lowest priority.
Priority Queue
• All the values are arranged in ascending order. Now, we will observe how the priority queue will look
after performing the following operations:

• poll(): This function will remove the highest priority element from the priority queue. In the above
priority queue, the '1' element has the highest priority, so it will be removed from the priority queue.

• add(2): This function will insert '2' element in a priority queue. As 2 is the smallest element among
all the numbers so it will obtain the highest priority.

• poll(): It will remove '2' element from the priority queue as it has the highest priority queue.

• add(5): It will insert 5 element after 4 as 5 is larger than 4 and lesser than 8, so it will obtain the
third highest priority in a priority queue.
Priority Queue
There are two types of priority queue:

1. Ascending order priority queue

2. Descending order priority queue


Ascending order priority queue

• In ascending order priority queue, a lower priority number is given as a higher priority in a priority.
For example, we take the numbers from 1 to 5 arranged in an ascending order like 1,2,3,4,5;
therefore, the smallest number, i.e., 1 is given as the highest priority in a priority queue.
Descending order priority queue

• In descending order priority queue, a higher priority number is given as a higher priority in a priority.
For example, we take the numbers from 1 to 5 arranged in descending order like 5, 4, 3, 2, 1;
therefore, the largest number, i.e., 5 is given as the highest priority in a priority queue.
#define SIZE 5 /* Size of Queue */ PriorityQ PQdelete()
int f=0,r=-1; /* Global declarations */ { /* Function for Delete operation */
typedef struct PRQ PriorityQ p;
{ if(Qempty()){ printf("nnUnderflow!!!!nn");
int ele; p.ele=-1;p.pr=-1;
int pr; return(p); }
}PriorityQ; else
{
PriorityQ PQ[SIZE]; p=PQ[f];
f=f+1;
PQinsert(int elem, int pre) return(p);
{ }
int i; /* Function for Insert operation */ }
if( Qfull()) printf("nn Overflow!!!!nn");
else int Qfull()
{ { /* Function to Check Queue Full */
i=r; if(r==SIZE-1) return 1;
++r; return 0;
while(PQ[i].pr >= pre && i >= 0) /* Find location for new elem */ }
{ int Qempty()
PQ[i+1]=PQ[i]; { /* Function to Check Queue Empty */
i--; } if(f > r) return 1;
PQ[i+1].ele=elem; return 0;
PQ[i+1].pr=pre; }
display()
switch(opn)
{ /* Function to display status of Queue */
{
int i;
if(Qempty()) printf(" n Empty Queuen");
case 1: printf("nnRead the element and its Priority?");
else
scanf("%d%d",&p.ele,&p.pr);
{ PQinsert(p.ele,p.pr); break;
printf("Front->"); case 2: p=PQdelete();
for(i=f;i<=r;i++) if( p.ele != -1)
printf("[%d,%d] ",PQ[i].ele,PQ[i].pr); printf("nnDeleted Element is %d n",p.ele);
printf("<-Rear"); break;
} case 3: printf("nnStatus of Queuenn");
} display(); break;
case 4: printf("nn Terminating nn"); break;
main()
default: printf("nnInvalid Option !!! Try Again !! nn");
{ /* Main Program */
break;
int opn;
}
PriorityQ p;
do
printf("nnnn Press a Key to Continue . . . ");
{
getch();
clrscr(); }while(opn != 4);
printf("n ### Priority Queue Operations(DSC order) ### nn"); }
printf("n Press 1-Insert, 2-Delete,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d",&opn);
Application of Priority Queue
• It is used in the Dijkstra's shortest path algorithm.

• It is used in prim's algorithm

• It is used in data compression techniques like Huffman code.

• It is used in heap sort.

• It is also used in operating system like priority scheduling, load balancing and interrupt handling.
Double Ended Queue (Deque)
•It is also a homogeneous list of elements in which insertion and deletion operations are performed from both the ends.

•That is, we can insert elements from the rear end or from the front ends.

•Hence it is called double-ended queue. It is commonly referred as a Deque.

•There are two types of Deque. These two types are due to the restrictions put to perform either the insertions or deletions only at

one end.

•There are:

• Input-restricted Deque.

• Output-restricted Deque.
Double Ended Queue (Deque)

• Below show a figure a empty Deque Q[5] which can accommodate five elements.

•There are:
• Input-restricted Deque: An input restricted Deque restricts the insertion of the elements at
one end only, the deletion of elements can be done at both the end of a queue.
Double Ended Queue (Deque)
• Output-restricted Deque: on the contrary, an Output-restricted Deque,
restricts the deletion of elements at one end only, and allows insertion
to be done at both the ends of a Deque.
Double Ended Queue (Deque)
• Operations on Deque:

1. insertFront(): Adds an item at the front of Deque.

2. insertLast(): Adds an item at the rear of Deque.

3. deleteFront(): Deletes an item from front of Deque.

4. deleteLast(): Deletes an item from rear of Deque.

• In addition to above operations, following operations are also supported


1. getFront(): Gets the front item from queue.
2. getRear(): Gets the last item from queue.
3. isEmpty(): Checks whether Deque is empty or not.
4. isFull(): Checks whether Deque is full or not.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy