SlideShare a Scribd company logo
Queue
1
Queue (Linear Queue)
• It is a linear data structure consisting of list of items.
• In queue, data elements are added at one end, called the rear and removed from another
end, called the front of the list.
• Two basic operations are associated with queue:
1. “Insert” operation is used to insert an element into a queue.
2. “Delete” operation is used to delete an element from a queue.
● FIFO list
• Example:
Queue: AAA, BBB, CCC, DDD, EEE
AAA BBB CCC DDD EEE
Rear
1 2 3 4 5 6 7
Front
EEE
DDD
CCC
BBB
AAA
Rear
Front
2
7
1
2
3
4
5
6
3
Algorithms for Insert and Delete Operations in Linear Queue
For Insert Operation
Insert-Queue(Queue, Rear, Front, N, Item)
Here, Queue is the place where to store data. 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. Here N is the maximum size of the Queue and finally, Item is
the new item to be added.
1. If Rear = N then Print: Overflow and Return. /*…Queue already filled..*/
2. Set Rear := Rear +1
3. Set Queue[Rear] := Item
4. Return.
4
For Delete Operation
Delete-Queue(Queue, Front, Rear, Item)
Here, Queue 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.
1. If Front = N+1 then Print: Underflow and Return. /*…Queue Empty
2. Set Item := Queue[Front]
3. Set Front := Front + 1
4. Return.
5
Example: Consider the following queue (linear queue).
Rear = 4 and Front = 1 and N = 7
10 50 30 40
(1) Insert 20. Now Rear = 5 and Front = 1
1 2 3 4 5 6 7
10 50 30 40 20
1 2 3 4 5 6 7
(2) Delete Front Element. Now Rear = 5 and Front = 2
50 30 40 20
1 2 3 4 5 6 7
(3) Delete Front Element. Now Rear = 5 and Front = 3
30 40 20
1 2 3 4 5 6 7
(4) Insert 60. Now Rear = 6 and Front = 3
30 40 20 60
1 2 3 4 5 6 7
6
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" element of the Queue becomes the
"Rear" most element, if and only if the "Front"
has moved forward. otherwise it will again be
a "Queue overflow" state.
7
Algorithms for Insert and Delete Operations in Circular Queue
For Insert Operation
Insert-Circular-Q(CQueue, Rear, Front, N, Item)
Here, CQueue is a circular queue where to store data. 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. Here N is the maximum
size of CQueue and finally, Item is the new item to be added. Initailly Rear = 0 and
Front = 0.
1. If Front = 0 and Rear = 0 then Set Front := 1 and go to step 4.
2. If Front =1 and Rear = N or Front = Rear + 1
then Print: “Circular Queue Overflow” and Return.
3. If Rear = N then Set Rear := 1 and go to step 5.
4. Set Rear := Rear + 1
5. Set CQueue [Rear] := Item.
6. Return
8
For Delete Operation
Delete-Circular-Q(CQueue, Front, Rear, Item)
Here, 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.
1. If Front = 0 then
Print: “Circular Queue Underflow” and Return. /*..Delete without Insertion
2. Set Item := CQueue [Front]
3. If Front = N then Set Front = 1 and Return.
4. If Front = Rear then Set Front = 0 and Rear = 0 and Return.
5. Set Front := Front + 1
6. Return.
9
Example: Consider the following circular queue with N = 5.
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
Rear
Front
Front
Front
Front
Front
10
7. Insert 100, Rear = 5, Front = 2.
8. Insert 40, Rear = 1, Front = 2.
9. Insert 140, Rear = 1, Front = 2.
As Front = Rear + 1, so Queue overflow.
10. Delete front, Rear = 1, Front = 3.
Front
Rear
FrontRear
Rear
Rear
Front
Front
11. Delete front, Rear = 1, Front = 4.
12. Delete front, Rear = 1, Front = 5.
Rear
Rear
Front
Front
Priority Queue
11
12
Priority Queue
• A priority queue is a collection of elements such that each element has been assigned a
priority.
● A priority queue supports inserting new priorities, and removing the highest priority.
● Two elements with the same priority are processed according to the order in which they
were added to the queue.
Representation of Priority Queue
• Each node in the list has three fields: an information field INFO, a priority number PRN
and a link number Link.
● A node X precedes a node Y in the list when X has higher priority than Y or both have
the same priority but X was added to the list before Y.
13
Deques
• A deque is a linear list in which data elements can be added or removed at either end but not
in the middle.
• This type of queue is also known as dequeue and double-ended queue.
• There are two types of DEQUE.
Input-Restricted Deque – Allow insertions at only one end of the list but deletions at
both ends of the list.
Output-Restricted Deque - Allow insertions at both ends but deletions at only one end of
the list.
•Example:
801002060
0 1 2 3 4 5 6 7
Figure: Example of a Deque
14
END!!!!

More Related Content

What's hot

What's hot (20)

Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Data Structures 2
Data Structures 2Data Structures 2
Data Structures 2
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue
QueueQueue
Queue
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Circular Queue data structure
Circular Queue data structureCircular Queue data structure
Circular Queue data structure
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Sorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesSorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - Notes
 
Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)
 
Array operations
Array operationsArray operations
Array operations
 
Priority queues and heap sorting
Priority queues and heap sortingPriority queues and heap sorting
Priority queues and heap sorting
 
Queue
QueueQueue
Queue
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
 
Queues-handouts
Queues-handoutsQueues-handouts
Queues-handouts
 
Quick sort
Quick sortQuick sort
Quick sort
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 

Similar to Queue

My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
Senthil Kumar
 
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
SadiaSharmin40
 

Similar to Queue (20)

My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular 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
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Detalied information of queue
Detalied information of queueDetalied information of queue
Detalied information of queue
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
10994103.ppt
10994103.ppt10994103.ppt
10994103.ppt
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
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)
 
Queues-and-CQueue-Implementation
Queues-and-CQueue-ImplementationQueues-and-CQueue-Implementation
Queues-and-CQueue-Implementation
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queues
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
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
 

More from Himadri Sen Gupta (9)

Report of industrial training
Report of industrial trainingReport of industrial training
Report of industrial training
 
Binary search trees (1)
Binary search trees (1)Binary search trees (1)
Binary search trees (1)
 
Graphs
GraphsGraphs
Graphs
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
14 recursion
14 recursion14 recursion
14 recursion
 
Tree
TreeTree
Tree
 
Heap
HeapHeap
Heap
 
Linked lists
Linked listsLinked lists
Linked lists
 
1311004
13110041311004
1311004
 

Recently uploaded

Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
Kamal Acharya
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
AbrahamGadissa
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 

Recently uploaded (20)

NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdf
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker project
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answer
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
Peek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfPeek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdf
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
 

Queue

  • 2. Queue (Linear Queue) • It is a linear data structure consisting of list of items. • In queue, data elements are added at one end, called the rear and removed from another end, called the front of the list. • Two basic operations are associated with queue: 1. “Insert” operation is used to insert an element into a queue. 2. “Delete” operation is used to delete an element from a queue. ● FIFO list • Example: Queue: AAA, BBB, CCC, DDD, EEE AAA BBB CCC DDD EEE Rear 1 2 3 4 5 6 7 Front EEE DDD CCC BBB AAA Rear Front 2 7 1 2 3 4 5 6
  • 3. 3 Algorithms for Insert and Delete Operations in Linear Queue For Insert Operation Insert-Queue(Queue, Rear, Front, N, Item) Here, Queue is the place where to store data. 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. Here N is the maximum size of the Queue and finally, Item is the new item to be added. 1. If Rear = N then Print: Overflow and Return. /*…Queue already filled..*/ 2. Set Rear := Rear +1 3. Set Queue[Rear] := Item 4. Return.
  • 4. 4 For Delete Operation Delete-Queue(Queue, Front, Rear, Item) Here, Queue 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. 1. If Front = N+1 then Print: Underflow and Return. /*…Queue Empty 2. Set Item := Queue[Front] 3. Set Front := Front + 1 4. Return.
  • 5. 5 Example: Consider the following queue (linear queue). Rear = 4 and Front = 1 and N = 7 10 50 30 40 (1) Insert 20. Now Rear = 5 and Front = 1 1 2 3 4 5 6 7 10 50 30 40 20 1 2 3 4 5 6 7 (2) Delete Front Element. Now Rear = 5 and Front = 2 50 30 40 20 1 2 3 4 5 6 7 (3) Delete Front Element. Now Rear = 5 and Front = 3 30 40 20 1 2 3 4 5 6 7 (4) Insert 60. Now Rear = 6 and Front = 3 30 40 20 60 1 2 3 4 5 6 7
  • 6. 6 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" element of the Queue becomes the "Rear" most element, if and only if the "Front" has moved forward. otherwise it will again be a "Queue overflow" state.
  • 7. 7 Algorithms for Insert and Delete Operations in Circular Queue For Insert Operation Insert-Circular-Q(CQueue, Rear, Front, N, Item) Here, CQueue is a circular queue where to store data. 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. Here N is the maximum size of CQueue and finally, Item is the new item to be added. Initailly Rear = 0 and Front = 0. 1. If Front = 0 and Rear = 0 then Set Front := 1 and go to step 4. 2. If Front =1 and Rear = N or Front = Rear + 1 then Print: “Circular Queue Overflow” and Return. 3. If Rear = N then Set Rear := 1 and go to step 5. 4. Set Rear := Rear + 1 5. Set CQueue [Rear] := Item. 6. Return
  • 8. 8 For Delete Operation Delete-Circular-Q(CQueue, Front, Rear, Item) Here, 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. 1. If Front = 0 then Print: “Circular Queue Underflow” and Return. /*..Delete without Insertion 2. Set Item := CQueue [Front] 3. If Front = N then Set Front = 1 and Return. 4. If Front = Rear then Set Front = 0 and Rear = 0 and Return. 5. Set Front := Front + 1 6. Return.
  • 9. 9 Example: Consider the following circular queue with N = 5. 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 Rear Front Front Front Front Front
  • 10. 10 7. Insert 100, Rear = 5, Front = 2. 8. Insert 40, Rear = 1, Front = 2. 9. Insert 140, Rear = 1, Front = 2. As Front = Rear + 1, so Queue overflow. 10. Delete front, Rear = 1, Front = 3. Front Rear FrontRear Rear Rear Front Front 11. Delete front, Rear = 1, Front = 4. 12. Delete front, Rear = 1, Front = 5. Rear Rear Front Front
  • 12. 12 Priority Queue • A priority queue is a collection of elements such that each element has been assigned a priority. ● A priority queue supports inserting new priorities, and removing the highest priority. ● Two elements with the same priority are processed according to the order in which they were added to the queue. Representation of Priority Queue • Each node in the list has three fields: an information field INFO, a priority number PRN and a link number Link. ● A node X precedes a node Y in the list when X has higher priority than Y or both have the same priority but X was added to the list before Y.
  • 13. 13 Deques • A deque is a linear list in which data elements can be added or removed at either end but not in the middle. • This type of queue is also known as dequeue and double-ended queue. • There are two types of DEQUE. Input-Restricted Deque – Allow insertions at only one end of the list but deletions at both ends of the list. Output-Restricted Deque - Allow insertions at both ends but deletions at only one end of the list. •Example: 801002060 0 1 2 3 4 5 6 7 Figure: Example of a Deque