SlideShare a Scribd company logo
1 of 20
1
Queues
• A queue is an ordered collection of items from which items may
be deleted at one end(front) and into which items are inserted at
the other end(rear).
• queue – similar to a supermarket checkout line
– first-in, first-out (FIFO)
– nodes are removed only from the head (front)
– nodes are inserted only at the tail(rear)
• The insert and remove operations are known as enqueue and
dequeue
Useful in computing
– Print spooling, packets in networks, file server requests
Three operations in the queue are
Insert(q,x) x=remove(q) empty(q)
2
Implementation of Queue
• Array implementation
#define MAXQUEUE 100
struct queue{
int items[MAXQUEUE];
int front,rear;
}q;
0
1
2
3
4
q.front=-1
q.rear=-1
q.items
0
1
2
3
4
q.items
A
B
C
q.front=0
q.rear=2
0
1
2
3
4
q.items
C q.front=q.rear=2
Queue is empty whenever q.rear<q.front
The number of elements in a queue is q.rear-q.front+1
3
Insert operation
QINSERT [ QUEUE, MAXSIZE, ITEM]
1. Initialization
Set Front = -1
Set Rear = -1
2. If Rear = = MAXSIZE-1 then Queue Overflow and Return
3. If Front = = -1 then
Front = 0
Rear = 0
Else
Rear = Rear + 1
4. Set Queue [Rear] = Item and Return
4
Delete Operation
QDELETE [ QUEUE, MAXSIZE, ITEM]
1. If Front = = -1 then Queue Underflow and Return
2. Set Item = Queue [ Front]
3. If Front = = Rear then
Set Front = -1
Set Rear = -1
Else
Front = Front + 1
4. Print, Deleted Item is “ITEM” and Return
5
Circular Queue
• A circular queue is one in which the insertion
of a new element is done at the very first
location of the queue if the last location of the
queue is full
• In other words, if we have a queue Q of say n
elements, then after inserting an element last
(i.e. in the n-1th) location of the array the next
element will be inserted at the very first
location (i.e. 0th location) of the array
6
7
Insertion operation in Circular queue
QINSERT [ QUEUE, N, Front, Rear, ITEM]
1. If Front ==0 and Rear == N-1
OR
Front == Rear+1, then Write Overflow queue and Return
2. If Front = = -1 then
Front = 0
Rear = 0
Else if Rear ==N-1 then
Set Rear = 0
Else
Rear = Rear + 1
3. Set Queue [Rear] = Item and Return
8
Deletion operation in circular queue
QDELETE [ QUEUE, N, Front, Rear, ITEM]
1. If Front = = -1 then Queue Underflow and Return
2. Set Item = Queue [ Front]
3. If Front = = Rear then
Set Front = -1
Set Rear = -1
Else if Front ==N-1 then
Set Front = 0
Else
Front = Front + 1
4. Print, Deleted Item is “ITEM” and Return
9
Advantage of Circular Queue over Linear
Queue
• A circular queue overcomes the problem of
unutilized space in linear queue implemented
as arrays.
10
Linked Representation of Queue
• A linked queue is queue implemented as a
linked list with two pointer variables FRONT
and REAR pointing to the nodes which is in the
FRONT and REAR of the queue
• The INFO fields of the list hold the elements of
the queue
• The LINK fields hold pointers to the
neighboring elements in the queue
11
Linked representation of Queue
Front Rear
Q
A B C D
In the case of insertion into a linked queue ,a
node borrowed from AVAIL list and carrying the
item is to be inserted added as the last node in
the linked representation of queue.
12
Rear
A B C D F
Rear
Q
Front
Inserting a node F in this linked Q
13
Insertion in Linked Representation of Queue
LINKQ_INSERT (INFO,LINK,FRONT,REAR,AVAIL,ITEM)
1. If AVAIL == NULL then write OVERFLOW and Exit
2. Set NEW = AVAIL and AVAIL = LINK[AVAIL]
3. Set INFO[NEW] = ITEM and LINK[NEW] = NULL
4. If (FRONT==NULL) then FRONT = REAR= NEW
Else Set LINK[REAR] = NEW and REAR = NEW
5. Exit
14
A B C D F
Rear
Front
Deleting a node A from this linked Q
Front
Q
15
Deletion in Linked Representation of Queue
LINKQ_DELETE
(INFO,LINK,FRONT,REAR,AVAIL,ITEM)
1. If FRONT == NULL then Write UNDERFLOW
and Exit
2. Set TEMP = FRONT
3. ITEM = INFO[TEMP]
4. FRONT = LINK[TEMP]
5. LINK[TEMP] = AVAIL and AVAIL = TEMP
6. Exit
16
Priority queues
• It is a collection of elements such that each
element has been assigned a priority such
that
-an element of higher priority
processed before any element of lower
priority
-two elements of equal priority are
processed according to the order in which
they were added to the queue.
17
Priority Queue
• FIFO rules of a queue are relaxed
• Customers or jobs with higher priority are
pushed to front (not rear) of queue
• To implement:
– use an ordinary linked list, which keeps the
items in order from the highest to lowest
priority
– use a treelike structure (heap)
18
B
D
E
A
C
G
F
2
4
4
1
2
5
4
6
7
4
9
1
3
10
0
8
11
12
0
INFO PRN LINK
2
AVAIL
5
START
One-Way List
Representation
of a Priority
Queue
19
Array Representation of a Priority Queue
2
1
0
5
4
2
3
0
1
4
FRONT REAR
1
2
3
4
5
1
2
3
4
5
B
F
A
C X
G
D E
1 2 3 4 5 6
Here X is added in List as priiority 2. That is why it is inserted after C.
20
Application of priority queue
• Application: Scheduling jobs on a workstation
• Priority Queue holds jobs to be performed
and their priorities
• When a job is finished or interrupted, highest-
priority job is chosen using Extract-Max
• New jobs can be added using Insert

More Related Content

What's hot (20)

Circular queue
Circular queueCircular queue
Circular queue
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Team 6
Team 6Team 6
Team 6
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queue
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Queue
QueueQueue
Queue
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 

Similar to FIFO Queues Explained

08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertexSadiaSharmin40
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - NotesOmprakash Chauhan
 
Queues-and-CQueue-Implementation
Queues-and-CQueue-ImplementationQueues-and-CQueue-Implementation
Queues-and-CQueue-Implementationshaik faroq
 
Computer Engineering Data Structure Queue.pptx
Computer Engineering Data Structure Queue.pptxComputer Engineering Data Structure Queue.pptx
Computer Engineering Data Structure Queue.pptxNingthoujamMahesh1
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptxsinghprpg
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Self-Employed
 
Queue data structures and operation on data structures
Queue data structures and operation on data structuresQueue data structures and operation on data structures
Queue data structures and operation on data structuresmuskans14
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationRAtna29
 

Similar to FIFO Queues Explained (20)

Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Queue
QueueQueue
Queue
 
08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex
 
Queues & ITS TYPES
Queues & ITS TYPESQueues & ITS TYPES
Queues & ITS TYPES
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Queues-and-CQueue-Implementation
Queues-and-CQueue-ImplementationQueues-and-CQueue-Implementation
Queues-and-CQueue-Implementation
 
Computer Engineering Data Structure Queue.pptx
Computer Engineering Data Structure Queue.pptxComputer Engineering Data Structure Queue.pptx
Computer Engineering Data Structure Queue.pptx
 
Queue
QueueQueue
Queue
 
QUEUE.pptx
QUEUE.pptxQUEUE.pptx
QUEUE.pptx
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
Queue
QueueQueue
Queue
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
Queue data structures and operation on data structures
Queue data structures and operation on data structuresQueue data structures and operation on data structures
Queue data structures and operation on data structures
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
QUEUES
QUEUESQUEUES
QUEUES
 

More from Mandeep Singh

9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & SearchingMandeep Singh
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
1. Data structures introduction
1. Data structures introduction1. Data structures introduction
1. Data structures introductionMandeep Singh
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingMandeep Singh
 

More from Mandeep Singh (11)

9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & Searching
 
8. Hash table
8. Hash table8. Hash table
8. Hash table
 
7. Spanning trees
7. Spanning trees7. Spanning trees
7. Spanning trees
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
1. Data structures introduction
1. Data structures introduction1. Data structures introduction
1. Data structures introduction
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented Programming
 
Ip6 tables in linux
Ip6 tables in linuxIp6 tables in linux
Ip6 tables in linux
 
Iptables in linux
Iptables in linuxIptables in linux
Iptables in linux
 

Recently uploaded

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsSachinPawar510423
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxsomshekarkn64
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 

Recently uploaded (20)

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documents
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptx
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 

FIFO Queues Explained

  • 1. 1 Queues • A queue is an ordered collection of items from which items may be deleted at one end(front) and into which items are inserted at the other end(rear). • queue – similar to a supermarket checkout line – first-in, first-out (FIFO) – nodes are removed only from the head (front) – nodes are inserted only at the tail(rear) • The insert and remove operations are known as enqueue and dequeue Useful in computing – Print spooling, packets in networks, file server requests Three operations in the queue are Insert(q,x) x=remove(q) empty(q)
  • 2. 2 Implementation of Queue • Array implementation #define MAXQUEUE 100 struct queue{ int items[MAXQUEUE]; int front,rear; }q; 0 1 2 3 4 q.front=-1 q.rear=-1 q.items 0 1 2 3 4 q.items A B C q.front=0 q.rear=2 0 1 2 3 4 q.items C q.front=q.rear=2 Queue is empty whenever q.rear<q.front The number of elements in a queue is q.rear-q.front+1
  • 3. 3 Insert operation QINSERT [ QUEUE, MAXSIZE, ITEM] 1. Initialization Set Front = -1 Set Rear = -1 2. If Rear = = MAXSIZE-1 then Queue Overflow and Return 3. If Front = = -1 then Front = 0 Rear = 0 Else Rear = Rear + 1 4. Set Queue [Rear] = Item and Return
  • 4. 4 Delete Operation QDELETE [ QUEUE, MAXSIZE, ITEM] 1. If Front = = -1 then Queue Underflow and Return 2. Set Item = Queue [ Front] 3. If Front = = Rear then Set Front = -1 Set Rear = -1 Else Front = Front + 1 4. Print, Deleted Item is “ITEM” and Return
  • 5. 5 Circular Queue • A circular queue is one in which the insertion of a new element is done at the very first location of the queue if the last location of the queue is full • In other words, if we have a queue Q of say n elements, then after inserting an element last (i.e. in the n-1th) location of the array the next element will be inserted at the very first location (i.e. 0th location) of the array
  • 6. 6
  • 7. 7 Insertion operation in Circular queue QINSERT [ QUEUE, N, Front, Rear, ITEM] 1. If Front ==0 and Rear == N-1 OR Front == Rear+1, then Write Overflow queue and Return 2. If Front = = -1 then Front = 0 Rear = 0 Else if Rear ==N-1 then Set Rear = 0 Else Rear = Rear + 1 3. Set Queue [Rear] = Item and Return
  • 8. 8 Deletion operation in circular queue QDELETE [ QUEUE, N, Front, Rear, ITEM] 1. If Front = = -1 then Queue Underflow and Return 2. Set Item = Queue [ Front] 3. If Front = = Rear then Set Front = -1 Set Rear = -1 Else if Front ==N-1 then Set Front = 0 Else Front = Front + 1 4. Print, Deleted Item is “ITEM” and Return
  • 9. 9 Advantage of Circular Queue over Linear Queue • A circular queue overcomes the problem of unutilized space in linear queue implemented as arrays.
  • 10. 10 Linked Representation of Queue • A linked queue is queue implemented as a linked list with two pointer variables FRONT and REAR pointing to the nodes which is in the FRONT and REAR of the queue • The INFO fields of the list hold the elements of the queue • The LINK fields hold pointers to the neighboring elements in the queue
  • 11. 11 Linked representation of Queue Front Rear Q A B C D In the case of insertion into a linked queue ,a node borrowed from AVAIL list and carrying the item is to be inserted added as the last node in the linked representation of queue.
  • 12. 12 Rear A B C D F Rear Q Front Inserting a node F in this linked Q
  • 13. 13 Insertion in Linked Representation of Queue LINKQ_INSERT (INFO,LINK,FRONT,REAR,AVAIL,ITEM) 1. If AVAIL == NULL then write OVERFLOW and Exit 2. Set NEW = AVAIL and AVAIL = LINK[AVAIL] 3. Set INFO[NEW] = ITEM and LINK[NEW] = NULL 4. If (FRONT==NULL) then FRONT = REAR= NEW Else Set LINK[REAR] = NEW and REAR = NEW 5. Exit
  • 14. 14 A B C D F Rear Front Deleting a node A from this linked Q Front Q
  • 15. 15 Deletion in Linked Representation of Queue LINKQ_DELETE (INFO,LINK,FRONT,REAR,AVAIL,ITEM) 1. If FRONT == NULL then Write UNDERFLOW and Exit 2. Set TEMP = FRONT 3. ITEM = INFO[TEMP] 4. FRONT = LINK[TEMP] 5. LINK[TEMP] = AVAIL and AVAIL = TEMP 6. Exit
  • 16. 16 Priority queues • It is a collection of elements such that each element has been assigned a priority such that -an element of higher priority processed before any element of lower priority -two elements of equal priority are processed according to the order in which they were added to the queue.
  • 17. 17 Priority Queue • FIFO rules of a queue are relaxed • Customers or jobs with higher priority are pushed to front (not rear) of queue • To implement: – use an ordinary linked list, which keeps the items in order from the highest to lowest priority – use a treelike structure (heap)
  • 19. 19 Array Representation of a Priority Queue 2 1 0 5 4 2 3 0 1 4 FRONT REAR 1 2 3 4 5 1 2 3 4 5 B F A C X G D E 1 2 3 4 5 6 Here X is added in List as priiority 2. That is why it is inserted after C.
  • 20. 20 Application of priority queue • Application: Scheduling jobs on a workstation • Priority Queue holds jobs to be performed and their priorities • When a job is finished or interrupted, highest- priority job is chosen using Extract-Max • New jobs can be added using Insert