SlideShare a Scribd company logo
1 of 22
Queues
Introduction to Queues
• A queue is a waiting line – seen in daily life
– A line of people waiting for a bank teller
– A line of cars at a toll both
• What other kinds of queues can you think of
The queue has a front and a rear.
$ $
Rear
Front
Front Rear
1 2 3 4
Front Rear
2 3 4
Front Rear
1 2 3
Add 4 to the Queue
Remove the element
from the Queue
Original Queue
In Queue
a. Items can be removed only at the front
b. Items can be added only at the other end, the back
Types Of Queues
• Linear Queue
• Circular Queue
• Double Ended Queue (Deque)
• Priority Queue
Double Ended Queue
Double ended queues, called deques for short, are a generalized form
of the queue. It is exactly like a queue except that elements can be
added to or removed from the head or the tail.
However, no element can be added and deleted from the middle.
In the computer’s memory, a deque is implemented using either a
circular array or a circular doubly linked list.
In a deque, two pointers are maintained, LEFT and RIGHT, which
point to either end of the deque. The elements in a deque extend from
the LEFT end to the RIGHT end and since it is circular, Dequeue[N–1]
is followed by Dequeue[0].
There are two variants of a double-ended queue. They
include :
• Input restricted deque In this, insertions can be done
only at one of the ends, while deletions can be done from
both ends.
• Output restricted deque In this deletions can be done only
at one of the ends, while insertions can be done on both
ends.
Priority Queues
A priority queue is a data structure in which each element is assigned
a priority. The priority of the element will be used to determine the
order in which the elements will be processed.
The general rules of processing the elements of a priority queue are
• An element with higher priority is processed before an element
with a lower priority.
• Two elements with the same priority are processed on a first-
come-first-served (FCFS) basis.
The Queue As an ADT
1. A queue is a sequence of data elements
2. Basic operations
a. Enqueue (add element to back)
b. Dequeue (remove element from front)
Enqueue & Dequeue operations
Array Implementation -Dequeue
When an item is taken from the queue, it always comes from the front.
This a dequeue operation.
Array Implementation -Enqueue
When an item is inserted into the queue, it always goes at the end (rear).
This a enqueue operation.
LINKED REPRESENTATION OF QUEUEs
Enqueue:
Dequeue
Circular Queue
09/10/08 14
Drawback of Linear Queue
• Once the queue is full, even though few elements from the front are deleted and
some occupied space is relieved, it is not possible to add anymore new elements,
as the rear has already reached the Queue’s rear most position.
Circular Queue
• This queue is not linear but circular.
• Its structure can be like the following figure:
Figure: Circular Queue having
Rear = 5 and Front = 0
• In circular queue, once the Queue is full the
"First" index of the Queue becomes the
"Rear" most index, if and only if the "Front"
element has moved forward. otherwise it will be
a "Queue overflow" state.
09/10/08 15
Algorithms for Insert Operations in Circular Queue
For Insert Operation
Insert-Circular-Q(CQueue, Rear, Front, N, Item)
Here, CQueue is a circular queue.
Rear represents the location in which the data element is to be inserted and
Front represents the location from which the data element is to be removed.
N is the maximum size of CQueue and
Item is the new item to be added.
Initailly Rear = -1 and Front = -1.
1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4.
2. If Front =0 and Rear = N-1 or Front = Rear + 1
then Print: “Circular Queue Overflow” and Return.
3. If Rear = N -1 then Set Rear := 0 and go to step 4.
4. Set CQueue [Rear] := Item. and Rear:=Rear + 1
5. Return
09/10/08 16
For Delete Operation
Delete-Circular-Q(CQueue, Front, Rear, Item)
CQueue is the place where data are stored.
Rear represents the location in which the data element is to be inserted and
Front represents the location from which the data element is to be removed.
Front element is assigned to Item.
Initially, Front = -1.
*..Delete without Insertion
1. If Front = -1 then Print: “Circular Queue Underflow” and Return.
2. Set Item := CQueue [Front]
3. If Front = N – 1 then Set Front = 0 and Return.
4. If Front = Rear then Set Front = Rear = -1 and Return.
5. Set Front := Front + 1
6. Return.
09/10/08 17
Example- ENQUEUE
Circular queue with N = 5.
Rear
1. Initially, Rear = 0, Front = 0.
2. Insert 10, Rear = 1, Front = 1.
3. Insert 50, Rear = 2, Front = 1.
4. Insert 20, Rear = 3, Front = 0.
5. Insert 70, Rear = 4, Front = 1.
6. Delete front, Rear = 4, Front = 2.
Rear
Rear
Rear
Rear
Front
Front
Front
Front
Front
Initailly Rear = -1 and Front = -1.
1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4.
2. If Front =0 and Rear = N-1 or Front = Rear + 1
then Print: “Circular Queue Overflow” and Return.
3. If Rear = N -1 then Set Rear := 0 and go to step 4.
4. Set CQueue [Rear] := Item. and Rear:=Rear + 1
5. Return
09/10/08 18
Example- ENQUEUE
Circular queue with N = 5.
Rear
1. Initially, Rear = -1, Front =-1
2. Insert 10, Rear = 0, Front = 0.
3. Insert 50, Rear = 1, Front = 0.
4. Insert 20, Rear = 2, Front = 0.
5. Insert 70, Rear = 3, Front = 0.
6. Delete front, Rear = 3, Front =1.
Rear
Rear
Rear
Rear
Front
Front
Front
Front
Front
Initailly Rear = -1 and Front = -1.
1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4.
2. If Front =0 and Rear = N-1 or Front = Rear + 1
then Print: “Circular Queue Overflow” and Return.
3. If Rear = N -1 then Set Rear := 0 and go to step 4.
4. Set CQueue [Rear] := Item. and Rear:=Rear + 1
5. Return
09/10/08 19
7. Insert 100, Rear = 4, Front = 1.
8. Insert 40, Rear = 0, Front = 1.
9. Insert 140, Rear = 0, Front = 1.
As Front = Rear + 1, so Queue overflow.
10. Delete front, Rear = 0, Front = 2.
Front
Rear
Front
Rear
Rear
Rear
Front
Front
11. Delete front, Rear = 0, Front = 3.
12. Delete front, Rear = 0, Front = 4.
Rear
Rear
Front
Front
Initailly Rear = -1 and Front = -1.
1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4.
2. If Front =0 and Rear = N-1 or Front = Rear + 1
then Print: “Circular Queue Overflow” and Return.
3. If Rear = N -1 then Set Rear := 0 and go to step 4.
4. Set CQueue [Rear] := Item. and Rear:=Rear + 1
5. Return
Initially, Front = -1.
1. If Front = -1 then Print: “Circular Queue Underflow” and Return.
2. Set Item := CQueue [Front]
3. If Front = N – 1 then Set Front = 0 and Return.
4. If Front = Rear then Set Front = Rear = -1 and Return.
5. Set Front := Front + 1
6. Return.
ENQUEUE/DEQUEUE
Circular queue with N = 5.
Double Ended Queue
Double ended queues, called deques for short, are a generalized form
of the queue. It is exactly like a queue except that elements can be
added to or removed from the head or the tail.
However, no element can be added and deleted from the middle.
In the computer’s memory, a deque is implemented using either a
circular array or a circular doubly linked list.
In a deque, two pointers are maintained, LEFT and RIGHT, which
point to either end of the deque. The elements in a deque extend from
the LEFT end to the RIGHT end and since it is circular, Dequeue[N–1]
is followed by Dequeue[0].
There are two variants of a double-ended queue. They
include :
• Input restricted deque In this, insertions can be done
only at one of the ends, while deletions can be done from
both ends.
• Output restricted deque In this deletions can be done only
at one of the ends, while insertions can be done on both
ends.
Priority Queues
A priority queue is a data structure in which each element is assigned
a priority. The priority of the element will be used to determine the
order in which the elements will be processed.
The general rules of processing the elements of a priority queue are
• An element with higher priority is processed before an element
with a lower priority.
• Two elements with the same priority are processed on a first-
come-first-served (FCFS) basis.

More Related Content

Similar to 08_Queues.pptx showing how que works given vertex (20)

LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Queue
QueueQueue
Queue
 
Detalied information of queue
Detalied information of queueDetalied information of queue
Detalied information of queue
 
Queue
QueueQueue
Queue
 
Data Structures 2
Data Structures 2Data Structures 2
Data Structures 2
 
Queue
QueueQueue
Queue
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
Circular queue
Circular queueCircular queue
Circular queue
 
Circular queue
Circular queueCircular queue
Circular queue
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Queue
QueueQueue
Queue
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 

More from SadiaSharmin40

16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structureSadiaSharmin40
 
chap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithmchap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithmSadiaSharmin40
 
brown.ppt for identifying rabin karp algo
brown.ppt for identifying rabin karp algobrown.ppt for identifying rabin karp algo
brown.ppt for identifying rabin karp algoSadiaSharmin40
 
huffman algoritm upload for understand.ppt
huffman algoritm upload for understand.ppthuffman algoritm upload for understand.ppt
huffman algoritm upload for understand.pptSadiaSharmin40
 
HuffmanStudent.ppt used to show how huffman code
HuffmanStudent.ppt used to show how huffman codeHuffmanStudent.ppt used to show how huffman code
HuffmanStudent.ppt used to show how huffman codeSadiaSharmin40
 
MergeSort.ppt shows how merge sort is done
MergeSort.ppt shows how merge sort is doneMergeSort.ppt shows how merge sort is done
MergeSort.ppt shows how merge sort is doneSadiaSharmin40
 
how to use counting sort algorithm to sort array
how to use counting sort algorithm to sort arrayhow to use counting sort algorithm to sort array
how to use counting sort algorithm to sort arraySadiaSharmin40
 
ER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdfER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdfSadiaSharmin40
 

More from SadiaSharmin40 (8)

16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure
 
chap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithmchap09alg.ppt for string matching algorithm
chap09alg.ppt for string matching algorithm
 
brown.ppt for identifying rabin karp algo
brown.ppt for identifying rabin karp algobrown.ppt for identifying rabin karp algo
brown.ppt for identifying rabin karp algo
 
huffman algoritm upload for understand.ppt
huffman algoritm upload for understand.ppthuffman algoritm upload for understand.ppt
huffman algoritm upload for understand.ppt
 
HuffmanStudent.ppt used to show how huffman code
HuffmanStudent.ppt used to show how huffman codeHuffmanStudent.ppt used to show how huffman code
HuffmanStudent.ppt used to show how huffman code
 
MergeSort.ppt shows how merge sort is done
MergeSort.ppt shows how merge sort is doneMergeSort.ppt shows how merge sort is done
MergeSort.ppt shows how merge sort is done
 
how to use counting sort algorithm to sort array
how to use counting sort algorithm to sort arrayhow to use counting sort algorithm to sort array
how to use counting sort algorithm to sort array
 
ER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdfER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdf
 

Recently uploaded

crisiscommunication-presentation in crisis management.pptx
crisiscommunication-presentation in crisis management.pptxcrisiscommunication-presentation in crisis management.pptx
crisiscommunication-presentation in crisis management.pptxSamahhassan30
 
CALL ON ➥8923113531 🔝Call Girls Charbagh Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Charbagh Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Charbagh Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Charbagh Lucknow best sexual serviceanilsa9823
 
ANIn Gurugram April 2024 |Can Agile and AI work together? by Pramodkumar Shri...
ANIn Gurugram April 2024 |Can Agile and AI work together? by Pramodkumar Shri...ANIn Gurugram April 2024 |Can Agile and AI work together? by Pramodkumar Shri...
ANIn Gurugram April 2024 |Can Agile and AI work together? by Pramodkumar Shri...AgileNetwork
 
LPC Operations Review PowerPoint | Operations Review
LPC Operations Review PowerPoint | Operations ReviewLPC Operations Review PowerPoint | Operations Review
LPC Operations Review PowerPoint | Operations Reviewthomas851723
 
CEO of Google, Sunder Pichai's biography
CEO of Google, Sunder Pichai's biographyCEO of Google, Sunder Pichai's biography
CEO of Google, Sunder Pichai's biographyHafizMuhammadAbdulla5
 
Fifteenth Finance Commission Presentation
Fifteenth Finance Commission PresentationFifteenth Finance Commission Presentation
Fifteenth Finance Commission Presentationmintusiprd
 
Day 0- Bootcamp Roadmap for PLC Bootcamp
Day 0- Bootcamp Roadmap for PLC BootcampDay 0- Bootcamp Roadmap for PLC Bootcamp
Day 0- Bootcamp Roadmap for PLC BootcampPLCLeadershipDevelop
 
VIP Kolkata Call Girl Rajarhat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Rajarhat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Rajarhat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Rajarhat 👉 8250192130 Available With Roomdivyansh0kumar0
 
Pooja Mehta 9167673311, Trusted Call Girls In NAVI MUMBAI Cash On Payment , V...
Pooja Mehta 9167673311, Trusted Call Girls In NAVI MUMBAI Cash On Payment , V...Pooja Mehta 9167673311, Trusted Call Girls In NAVI MUMBAI Cash On Payment , V...
Pooja Mehta 9167673311, Trusted Call Girls In NAVI MUMBAI Cash On Payment , V...Pooja Nehwal
 
Reflecting, turning experience into insight
Reflecting, turning experience into insightReflecting, turning experience into insight
Reflecting, turning experience into insightWayne Abrahams
 
LPC Warehouse Management System For Clients In The Business Sector
LPC Warehouse Management System For Clients In The Business SectorLPC Warehouse Management System For Clients In The Business Sector
LPC Warehouse Management System For Clients In The Business Sectorthomas851723
 
Board Diversity Initiaive Launch Presentation
Board Diversity Initiaive Launch PresentationBoard Diversity Initiaive Launch Presentation
Board Diversity Initiaive Launch Presentationcraig524401
 
Introduction to LPC - Facility Design And Re-Engineering
Introduction to LPC - Facility Design And Re-EngineeringIntroduction to LPC - Facility Design And Re-Engineering
Introduction to LPC - Facility Design And Re-Engineeringthomas851723
 

Recently uploaded (17)

crisiscommunication-presentation in crisis management.pptx
crisiscommunication-presentation in crisis management.pptxcrisiscommunication-presentation in crisis management.pptx
crisiscommunication-presentation in crisis management.pptx
 
CALL ON ➥8923113531 🔝Call Girls Charbagh Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Charbagh Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Charbagh Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Charbagh Lucknow best sexual service
 
ANIn Gurugram April 2024 |Can Agile and AI work together? by Pramodkumar Shri...
ANIn Gurugram April 2024 |Can Agile and AI work together? by Pramodkumar Shri...ANIn Gurugram April 2024 |Can Agile and AI work together? by Pramodkumar Shri...
ANIn Gurugram April 2024 |Can Agile and AI work together? by Pramodkumar Shri...
 
LPC Operations Review PowerPoint | Operations Review
LPC Operations Review PowerPoint | Operations ReviewLPC Operations Review PowerPoint | Operations Review
LPC Operations Review PowerPoint | Operations Review
 
CEO of Google, Sunder Pichai's biography
CEO of Google, Sunder Pichai's biographyCEO of Google, Sunder Pichai's biography
CEO of Google, Sunder Pichai's biography
 
Fifteenth Finance Commission Presentation
Fifteenth Finance Commission PresentationFifteenth Finance Commission Presentation
Fifteenth Finance Commission Presentation
 
Day 0- Bootcamp Roadmap for PLC Bootcamp
Day 0- Bootcamp Roadmap for PLC BootcampDay 0- Bootcamp Roadmap for PLC Bootcamp
Day 0- Bootcamp Roadmap for PLC Bootcamp
 
Rohini Sector 16 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 16 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 16 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 16 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
VIP Kolkata Call Girl Rajarhat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Rajarhat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Rajarhat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Rajarhat 👉 8250192130 Available With Room
 
Pooja Mehta 9167673311, Trusted Call Girls In NAVI MUMBAI Cash On Payment , V...
Pooja Mehta 9167673311, Trusted Call Girls In NAVI MUMBAI Cash On Payment , V...Pooja Mehta 9167673311, Trusted Call Girls In NAVI MUMBAI Cash On Payment , V...
Pooja Mehta 9167673311, Trusted Call Girls In NAVI MUMBAI Cash On Payment , V...
 
Reflecting, turning experience into insight
Reflecting, turning experience into insightReflecting, turning experience into insight
Reflecting, turning experience into insight
 
LPC Warehouse Management System For Clients In The Business Sector
LPC Warehouse Management System For Clients In The Business SectorLPC Warehouse Management System For Clients In The Business Sector
LPC Warehouse Management System For Clients In The Business Sector
 
Becoming an Inclusive Leader - Bernadette Thompson
Becoming an Inclusive Leader - Bernadette ThompsonBecoming an Inclusive Leader - Bernadette Thompson
Becoming an Inclusive Leader - Bernadette Thompson
 
Call Girls Service Tilak Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SERVICE
Call Girls Service Tilak Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SERVICECall Girls Service Tilak Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SERVICE
Call Girls Service Tilak Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SERVICE
 
Board Diversity Initiaive Launch Presentation
Board Diversity Initiaive Launch PresentationBoard Diversity Initiaive Launch Presentation
Board Diversity Initiaive Launch Presentation
 
Introduction to LPC - Facility Design And Re-Engineering
Introduction to LPC - Facility Design And Re-EngineeringIntroduction to LPC - Facility Design And Re-Engineering
Introduction to LPC - Facility Design And Re-Engineering
 
sauth delhi call girls in Defence Colony🔝 9953056974 🔝 escort Service
sauth delhi call girls in Defence Colony🔝 9953056974 🔝 escort Servicesauth delhi call girls in Defence Colony🔝 9953056974 🔝 escort Service
sauth delhi call girls in Defence Colony🔝 9953056974 🔝 escort Service
 

08_Queues.pptx showing how que works given vertex

  • 2. Introduction to Queues • A queue is a waiting line – seen in daily life – A line of people waiting for a bank teller – A line of cars at a toll both • What other kinds of queues can you think of The queue has a front and a rear. $ $ Rear Front
  • 3. Front Rear 1 2 3 4 Front Rear 2 3 4 Front Rear 1 2 3 Add 4 to the Queue Remove the element from the Queue Original Queue In Queue a. Items can be removed only at the front b. Items can be added only at the other end, the back
  • 4. Types Of Queues • Linear Queue • Circular Queue • Double Ended Queue (Deque) • Priority Queue
  • 5. Double Ended Queue Double ended queues, called deques for short, are a generalized form of the queue. It is exactly like a queue except that elements can be added to or removed from the head or the tail. However, no element can be added and deleted from the middle. In the computer’s memory, a deque is implemented using either a circular array or a circular doubly linked list. In a deque, two pointers are maintained, LEFT and RIGHT, which point to either end of the deque. The elements in a deque extend from the LEFT end to the RIGHT end and since it is circular, Dequeue[N–1] is followed by Dequeue[0].
  • 6. There are two variants of a double-ended queue. They include : • Input restricted deque In this, insertions can be done only at one of the ends, while deletions can be done from both ends. • Output restricted deque In this deletions can be done only at one of the ends, while insertions can be done on both ends.
  • 7. Priority Queues A priority queue is a data structure in which each element is assigned a priority. The priority of the element will be used to determine the order in which the elements will be processed. The general rules of processing the elements of a priority queue are • An element with higher priority is processed before an element with a lower priority. • Two elements with the same priority are processed on a first- come-first-served (FCFS) basis.
  • 8. The Queue As an ADT 1. A queue is a sequence of data elements 2. Basic operations a. Enqueue (add element to back) b. Dequeue (remove element from front) Enqueue & Dequeue operations
  • 9. Array Implementation -Dequeue When an item is taken from the queue, it always comes from the front. This a dequeue operation.
  • 10. Array Implementation -Enqueue When an item is inserted into the queue, it always goes at the end (rear). This a enqueue operation.
  • 11. LINKED REPRESENTATION OF QUEUEs Enqueue:
  • 14. 09/10/08 14 Drawback of Linear Queue • Once the queue is full, even though few elements from the front are deleted and some occupied space is relieved, it is not possible to add anymore new elements, as the rear has already reached the Queue’s rear most position. Circular Queue • This queue is not linear but circular. • Its structure can be like the following figure: Figure: Circular Queue having Rear = 5 and Front = 0 • In circular queue, once the Queue is full the "First" index of the Queue becomes the "Rear" most index, if and only if the "Front" element has moved forward. otherwise it will be a "Queue overflow" state.
  • 15. 09/10/08 15 Algorithms for Insert Operations in Circular Queue For Insert Operation Insert-Circular-Q(CQueue, Rear, Front, N, Item) Here, CQueue is a circular queue. Rear represents the location in which the data element is to be inserted and Front represents the location from which the data element is to be removed. N is the maximum size of CQueue and Item is the new item to be added. Initailly Rear = -1 and Front = -1. 1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4. 2. If Front =0 and Rear = N-1 or Front = Rear + 1 then Print: “Circular Queue Overflow” and Return. 3. If Rear = N -1 then Set Rear := 0 and go to step 4. 4. Set CQueue [Rear] := Item. and Rear:=Rear + 1 5. Return
  • 16. 09/10/08 16 For Delete Operation Delete-Circular-Q(CQueue, Front, Rear, Item) CQueue is the place where data are stored. Rear represents the location in which the data element is to be inserted and Front represents the location from which the data element is to be removed. Front element is assigned to Item. Initially, Front = -1. *..Delete without Insertion 1. If Front = -1 then Print: “Circular Queue Underflow” and Return. 2. Set Item := CQueue [Front] 3. If Front = N – 1 then Set Front = 0 and Return. 4. If Front = Rear then Set Front = Rear = -1 and Return. 5. Set Front := Front + 1 6. Return.
  • 17. 09/10/08 17 Example- ENQUEUE Circular queue with N = 5. Rear 1. Initially, Rear = 0, Front = 0. 2. Insert 10, Rear = 1, Front = 1. 3. Insert 50, Rear = 2, Front = 1. 4. Insert 20, Rear = 3, Front = 0. 5. Insert 70, Rear = 4, Front = 1. 6. Delete front, Rear = 4, Front = 2. Rear Rear Rear Rear Front Front Front Front Front Initailly Rear = -1 and Front = -1. 1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4. 2. If Front =0 and Rear = N-1 or Front = Rear + 1 then Print: “Circular Queue Overflow” and Return. 3. If Rear = N -1 then Set Rear := 0 and go to step 4. 4. Set CQueue [Rear] := Item. and Rear:=Rear + 1 5. Return
  • 18. 09/10/08 18 Example- ENQUEUE Circular queue with N = 5. Rear 1. Initially, Rear = -1, Front =-1 2. Insert 10, Rear = 0, Front = 0. 3. Insert 50, Rear = 1, Front = 0. 4. Insert 20, Rear = 2, Front = 0. 5. Insert 70, Rear = 3, Front = 0. 6. Delete front, Rear = 3, Front =1. Rear Rear Rear Rear Front Front Front Front Front Initailly Rear = -1 and Front = -1. 1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4. 2. If Front =0 and Rear = N-1 or Front = Rear + 1 then Print: “Circular Queue Overflow” and Return. 3. If Rear = N -1 then Set Rear := 0 and go to step 4. 4. Set CQueue [Rear] := Item. and Rear:=Rear + 1 5. Return
  • 19. 09/10/08 19 7. Insert 100, Rear = 4, Front = 1. 8. Insert 40, Rear = 0, Front = 1. 9. Insert 140, Rear = 0, Front = 1. As Front = Rear + 1, so Queue overflow. 10. Delete front, Rear = 0, Front = 2. Front Rear Front Rear Rear Rear Front Front 11. Delete front, Rear = 0, Front = 3. 12. Delete front, Rear = 0, Front = 4. Rear Rear Front Front Initailly Rear = -1 and Front = -1. 1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4. 2. If Front =0 and Rear = N-1 or Front = Rear + 1 then Print: “Circular Queue Overflow” and Return. 3. If Rear = N -1 then Set Rear := 0 and go to step 4. 4. Set CQueue [Rear] := Item. and Rear:=Rear + 1 5. Return Initially, Front = -1. 1. If Front = -1 then Print: “Circular Queue Underflow” and Return. 2. Set Item := CQueue [Front] 3. If Front = N – 1 then Set Front = 0 and Return. 4. If Front = Rear then Set Front = Rear = -1 and Return. 5. Set Front := Front + 1 6. Return. ENQUEUE/DEQUEUE Circular queue with N = 5.
  • 20. Double Ended Queue Double ended queues, called deques for short, are a generalized form of the queue. It is exactly like a queue except that elements can be added to or removed from the head or the tail. However, no element can be added and deleted from the middle. In the computer’s memory, a deque is implemented using either a circular array or a circular doubly linked list. In a deque, two pointers are maintained, LEFT and RIGHT, which point to either end of the deque. The elements in a deque extend from the LEFT end to the RIGHT end and since it is circular, Dequeue[N–1] is followed by Dequeue[0].
  • 21. There are two variants of a double-ended queue. They include : • Input restricted deque In this, insertions can be done only at one of the ends, while deletions can be done from both ends. • Output restricted deque In this deletions can be done only at one of the ends, while insertions can be done on both ends.
  • 22. Priority Queues A priority queue is a data structure in which each element is assigned a priority. The priority of the element will be used to determine the order in which the elements will be processed. The general rules of processing the elements of a priority queue are • An element with higher priority is processed before an element with a lower priority. • Two elements with the same priority are processed on a first- come-first-served (FCFS) basis.