SlideShare a Scribd company logo
1 of 26
Queue
PREPARED BY:-
BHAVESH PARMAR(15ITS304)
 Queue
A queue is a linear Data structure in which insertion is performed at one end called rear and deletion is
performed at another end of the list is called front.
Type of Queue:
1.Simple Queue
2.Circularqueue
3.Dequeue
4.Priority Queue
 Representation of Queue
 The information in such a list is proceeds in the same order as it was received.
 Since insertion is performed at one end and deletion is performed at another end the element which is inserted
first is first to delete. So it is also known as First in First out (FIFO) or First Come First Serve (FCFS) data structure.
 Examples of Queue
A raw of students at registration counter.
Line of cars waiting to proceeds in some direction at traffic signal.
 Application of Queue
o A queue is the natural data structure for a system to serve its incoming requests. Most of
the process scheduling or disk scheduling algorithms in operating systems use queues.
o Computer hardware like a processor or a network card also maintain buffers in the form of
queues for incoming resource requests. A stack-like data structure causes starvation of the
first requests, and is not applicable in such cases.
o A mailbox or port to save messages to communicate between two users or processes in a
system is essentially a queue-like structure.
o Queue is widely used in Simulation.
 Operation on Simple Queue:
(1) Insert an element in to queue
(2) Delete an element from queue
1. Algorithm to insert an element in queue
PROCEDURE QINSERT (Q, F, R, N, Y)
• This function insert an element into the queue
• The Queue is represented by vector Q which contains N elements.
• F is a pointer which points to the front end
• R is a pointer which points to the rear end
• Y is the element to be inserted.
1) [Overflow?]
If R≥N then
Write (‘Overflow’)
2) [Increment rear pointer ]
RR+1
3) [Insert element]
Q[R]Y
4 ) [Is front pointer properly set?]
If F=0 then
F1
Return
2. Algorithm to delete an element from the queue
PROCEDURE QDELETE (Q, F, R)
• This function delete an element into the queue
• The Queue is represented by vector Q which contains N elements.
• F is a pointer which points to the front end
• R is a pointer which points to the rear end
• Y is the element to be inserted.
1) [Overflow?]
If R≥N then
Write (‘Overflow’)
2) [Delete element]
YQ [F]
3) [Queue empty?]
If F =R Then
F0
R0
Else FF+1
4) [Return element]
Return (y)
 For example consider following
operations:
o As shown in figure (a), we insert three elements 5, 10, 15 in simple queue.
o After that we delete 5 and 10 as shown in figure (b). Now even we have a free memory
space we cannot use that memory space. So simple queue results in wastage of memory
space.
o This problem can be solved by using circular queue.
 Circular Queue:
A circular queue is a queue in which elements are added and removed in a circular
fashion / manner, is called Circular Queue.
:
 Represent of circular Queue:
o As shown in figure (a), we insert eight elements 10, 20,30,40,50,60,70,80 in simple
queue. After that we delete 10, 20 and 30 as shown in figure (b). Now we have a free
memory space in circular queue and we can use that memory space by incrementing
rear pointer by 1(rear=0).
 Operations on Circular Queue
(1) Insert new element in to circular queue
(2) Delete element from circular queue
(1) Algorithm to insert element in circular queue
PROCEDURE CQINSERT (Q, F, R, N, Y)
o This function inserts an element in to circular queue.
o The Queue is represented by vector Q which contains N elements.
o F is a pointer which points to the front end
o R is a pointer which points to the rear end
o Y is the element to be inserted.
1) [Reset rear pointer?]
If R = N then
R 1
Else
RR + 1
2) [Overflow?]
if F= R then
Write (“queue overflow”)
Return
3) [Insert element]
Q[R]Y
4) [Is front pointer properly set?]
If F = 0 then
F1
Return
(2) Algorithm to delete element from circular queue.
PROCEDURE CQDELETE (Q, F, R,N)
o This function deletes an element from circular queue
o The Queue is represented by vector Q which contains N elements.
o F is a pointer which points to the front end
o R is a pointer which points to the rear end
o Y is temporary variable.
1) [Underflow]
If F = 0 then
Write (Underflow“)
2) [Delete element]
YQ[F]
3) [Queue empty?}
f F = R then
FßRß0
Return(Y)
4) [Increment front pointer}
If F = N then
F1
Else
FF+1
Return (Y)
 Dequeue:
Dequeue is a linier list in which insertion and deletion operation are performed at
either end of the queue.
Dequeue is also known double ended Queue…
Dequeue is shown below:
Operation on Dequeue
(1) Insert an element in to Dequeue
(2) Delete an element from Dequeue
 There are two types of Dequeue:
o Input restricted Dequeue: in input restricted Dequeue insertion operation is
performed at only one end of the queue.
o Output restricted Dequeue: in output restricted Dequeue deletion operation is
performed at only one end of the queue.
1.Insert operation in Dequeue
qinsert_beg(q,val)
1. If ( q  Rear = Max-1 AND q  FRONT=0]
Print “overflow Queue is full” and goto step 4
End If
2. q Front=-1
set q  Front=q  Rear=0
Set qDqueue [q  Front]= val
End If
3. If qRear != MAX=-1
Set num_item=qRear-qFRONT +1
Set i=qRear+1
set j=1
while j<=num_item
Set q  Dque[i]=q  Dque[i-1]
Set i=i-1
Set j=j+1
End While
Set qDque[i]=val
Set qFront=i
Set qRear =qRear +1
Else
Set qFront=qFront-1
Set qDque[qFront]=val
End if
4. End
2.Insert operation At the end of queue
Procedure qinsert_end(q,val)
1. If (qRear =Max-1 And qFront =0)
print “overflow” goto step4
End If
2. If qFront=-1
set q  Front=q  Rear=0
Set qDqueue [q  Front]= val and goto step4
End If
3. If q Rear = MAX-1
Set i=q  Front-1
While i< q  Rear
Set q Dque[i] =qDque[i+1]
Set i= i+1
End while
Set q  Dque[qRear]=val
Set q  Front=qFront-1
Else
Set q  Rear=qRear+1
Set q  Dque[qRear]=val
End IF
4. End
3.Delete operation in the beginning position
Procedure Qdelete_beg(q)
1. If q  Front = -1
Print “Underflow”
Return 0 and go to step 5
End If
2. Set del_val=q  Dque[qFront]
3.. If q  Front =q  Rear
Set q  front = q  Rear = -1
Else
Set qFront =q  front +1
End if
4. Return del_val
5. End
4.Delete operation At end of queue
Procedure delete_end(q)
1. If q  Front = -1
Print “Underflow”
Return 0 and go to step 5
End if
2. Set q del_val=q  Dque[qrear]
3. q  front = q  Rear
Set qfront = q Rear -1
Else
set q Rear = qrear-1
If qrear = -1
set qfront = -1
End if
End if
4. Return del_val
5.End
 Priority Queue:
A queue in which we are able to insert items or remove items from any position
based on some priority is known as priority queue.
R1 R2 …… Ri-1 O1 O2 ……. Oj-1 B1 B2 …… Bk-1
1 2 …… 1 2 2 …….. 2 3 3 …… 3
:
o Figure represents a priority queue of jobs waiting to use a computer
o Priorities of 1, 2, 3 have been attached to jobs of real time, on-line and batch
respectively.
o Figure shows how the single priority queue can be visualized as three separate
queues.
o When elements are inserted, they are always added at the end of one of the
queues as determined by the priorities
o Elements in the second queue are deleted only when the first queue is empty.
o Elements in the third queue are deleted only when the first and second queue are
empty.
 Operation on Priority Queue
1) Insert operation in priority Queue
2) Delete operation in priority Queue
1.Insert operation into priority Queue
Procedure Qinsert (q,val,prno) //prno is the priority of val
1). (qRear[prno]=MAX-1 AND qFront[prno]=0) OR
(qRear[prno]+1=qFront[prno]=0)
Print(“overflow queue”) and goto step-5
End If
2). If qRear[prno-1]=Max-1
set qRear[prno-1]= 0
Else
Set qRear[prno-1]=qRear[prno-1]+1
End if
3). Set qCqueue[prno-1] [qRear[prno-1]] =val.
4). If qFront[prno-1]= -1
Set qFront[prno-1] = 0
End if
5). End
2.delete operation on priority Queue
Procedure Qdelete(q)
1). Set flag=0,i=0
2). while I =MAX-1
If NOT(q  Front[prno])= -1
Set flag=1
Set del_val=q  Cqueue[1][q  Front[i]]
If qFront[ i ]=qRear[i]
Set q  Front[i] = qRear[i]=-1
Else If q  Rear[prno-1]=Max-1
Set q  Rear[prno-1]= 0
Else
Set q  Rear[prno-1]=q  Rear[prno-1]+1
End if
End if
Break //jump out from while loop
End if
Set i= i+1
End While
3). If FLAG=0
print(“underflow Queue”)
Return 0 and go to step 4
Else
Return Del_val
End IF
4). End
Queue

More Related Content

What's hot (20)

Circular queue
Circular queueCircular queue
Circular queue
 
Team 6
Team 6Team 6
Team 6
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
QUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING CQUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING C
 
Queue
QueueQueue
Queue
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Stacks fundamentals
Stacks fundamentalsStacks fundamentals
Stacks fundamentals
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Queue
QueueQueue
Queue
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
Queue
QueueQueue
Queue
 

Similar to Queue (20)

Queue
QueueQueue
Queue
 
05 queues
05 queues05 queues
05 queues
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
QUEUES
QUEUESQUEUES
QUEUES
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Queues & ITS TYPES
Queues & ITS TYPESQueues & ITS TYPES
Queues & ITS TYPES
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
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)
 
sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
 
Queue
QueueQueue
Queue
 
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
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
QUEUE.pptx
QUEUE.pptxQUEUE.pptx
QUEUE.pptx
 
Queue
QueueQueue
Queue
 

More from Bhavesh Parmar

More from Bhavesh Parmar (10)

Inerrupt
InerruptInerrupt
Inerrupt
 
Mpeg7
Mpeg7Mpeg7
Mpeg7
 
Master page
Master pageMaster page
Master page
 
Training and placement reportmsword
Training and placement reportmswordTraining and placement reportmsword
Training and placement reportmsword
 
acknowledgement
acknowledgementacknowledgement
acknowledgement
 
index of project
index of projectindex of project
index of project
 
Training and placement
Training and placementTraining and placement
Training and placement
 
4 index
4 index4 index
4 index
 
3 page acknowledgement1
3 page acknowledgement13 page acknowledgement1
3 page acknowledgement1
 
Training and placement ppt
Training and placement pptTraining and placement ppt
Training and placement ppt
 

Recently uploaded

Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
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
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 

Recently uploaded (20)

Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
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
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
🔝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...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
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
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 

Queue

  • 2.  Queue A queue is a linear Data structure in which insertion is performed at one end called rear and deletion is performed at another end of the list is called front. Type of Queue: 1.Simple Queue 2.Circularqueue 3.Dequeue 4.Priority Queue  Representation of Queue  The information in such a list is proceeds in the same order as it was received.  Since insertion is performed at one end and deletion is performed at another end the element which is inserted first is first to delete. So it is also known as First in First out (FIFO) or First Come First Serve (FCFS) data structure.
  • 3.  Examples of Queue A raw of students at registration counter. Line of cars waiting to proceeds in some direction at traffic signal.  Application of Queue o A queue is the natural data structure for a system to serve its incoming requests. Most of the process scheduling or disk scheduling algorithms in operating systems use queues. o Computer hardware like a processor or a network card also maintain buffers in the form of queues for incoming resource requests. A stack-like data structure causes starvation of the first requests, and is not applicable in such cases. o A mailbox or port to save messages to communicate between two users or processes in a system is essentially a queue-like structure. o Queue is widely used in Simulation.
  • 4.  Operation on Simple Queue: (1) Insert an element in to queue (2) Delete an element from queue 1. Algorithm to insert an element in queue PROCEDURE QINSERT (Q, F, R, N, Y) • This function insert an element into the queue • The Queue is represented by vector Q which contains N elements. • F is a pointer which points to the front end • R is a pointer which points to the rear end • Y is the element to be inserted. 1) [Overflow?] If R≥N then Write (‘Overflow’)
  • 5. 2) [Increment rear pointer ] RR+1 3) [Insert element] Q[R]Y 4 ) [Is front pointer properly set?] If F=0 then F1 Return
  • 6. 2. Algorithm to delete an element from the queue PROCEDURE QDELETE (Q, F, R) • This function delete an element into the queue • The Queue is represented by vector Q which contains N elements. • F is a pointer which points to the front end • R is a pointer which points to the rear end • Y is the element to be inserted. 1) [Overflow?] If R≥N then Write (‘Overflow’) 2) [Delete element] YQ [F] 3) [Queue empty?] If F =R Then F0 R0 Else FF+1 4) [Return element] Return (y)
  • 7.  For example consider following operations: o As shown in figure (a), we insert three elements 5, 10, 15 in simple queue. o After that we delete 5 and 10 as shown in figure (b). Now even we have a free memory space we cannot use that memory space. So simple queue results in wastage of memory space. o This problem can be solved by using circular queue.
  • 8.  Circular Queue: A circular queue is a queue in which elements are added and removed in a circular fashion / manner, is called Circular Queue. :  Represent of circular Queue:
  • 9. o As shown in figure (a), we insert eight elements 10, 20,30,40,50,60,70,80 in simple queue. After that we delete 10, 20 and 30 as shown in figure (b). Now we have a free memory space in circular queue and we can use that memory space by incrementing rear pointer by 1(rear=0).  Operations on Circular Queue (1) Insert new element in to circular queue (2) Delete element from circular queue
  • 10. (1) Algorithm to insert element in circular queue PROCEDURE CQINSERT (Q, F, R, N, Y) o This function inserts an element in to circular queue. o The Queue is represented by vector Q which contains N elements. o F is a pointer which points to the front end o R is a pointer which points to the rear end o Y is the element to be inserted. 1) [Reset rear pointer?] If R = N then R 1 Else RR + 1 2) [Overflow?] if F= R then Write (“queue overflow”) Return 3) [Insert element] Q[R]Y
  • 11. 4) [Is front pointer properly set?] If F = 0 then F1 Return
  • 12. (2) Algorithm to delete element from circular queue. PROCEDURE CQDELETE (Q, F, R,N) o This function deletes an element from circular queue o The Queue is represented by vector Q which contains N elements. o F is a pointer which points to the front end o R is a pointer which points to the rear end o Y is temporary variable. 1) [Underflow] If F = 0 then Write (Underflow“) 2) [Delete element] YQ[F] 3) [Queue empty?} f F = R then FßRß0 Return(Y)
  • 13. 4) [Increment front pointer} If F = N then F1 Else FF+1 Return (Y)
  • 14.  Dequeue: Dequeue is a linier list in which insertion and deletion operation are performed at either end of the queue. Dequeue is also known double ended Queue… Dequeue is shown below:
  • 15. Operation on Dequeue (1) Insert an element in to Dequeue (2) Delete an element from Dequeue  There are two types of Dequeue: o Input restricted Dequeue: in input restricted Dequeue insertion operation is performed at only one end of the queue. o Output restricted Dequeue: in output restricted Dequeue deletion operation is performed at only one end of the queue.
  • 16. 1.Insert operation in Dequeue qinsert_beg(q,val) 1. If ( q  Rear = Max-1 AND q  FRONT=0] Print “overflow Queue is full” and goto step 4 End If 2. q Front=-1 set q  Front=q  Rear=0 Set qDqueue [q  Front]= val End If 3. If qRear != MAX=-1 Set num_item=qRear-qFRONT +1 Set i=qRear+1 set j=1 while j<=num_item Set q  Dque[i]=q  Dque[i-1] Set i=i-1
  • 17. Set j=j+1 End While Set qDque[i]=val Set qFront=i Set qRear =qRear +1 Else Set qFront=qFront-1 Set qDque[qFront]=val End if 4. End 2.Insert operation At the end of queue Procedure qinsert_end(q,val) 1. If (qRear =Max-1 And qFront =0) print “overflow” goto step4 End If
  • 18. 2. If qFront=-1 set q  Front=q  Rear=0 Set qDqueue [q  Front]= val and goto step4 End If 3. If q Rear = MAX-1 Set i=q  Front-1 While i< q  Rear Set q Dque[i] =qDque[i+1] Set i= i+1 End while Set q  Dque[qRear]=val Set q  Front=qFront-1 Else Set q  Rear=qRear+1 Set q  Dque[qRear]=val End IF 4. End
  • 19. 3.Delete operation in the beginning position Procedure Qdelete_beg(q) 1. If q  Front = -1 Print “Underflow” Return 0 and go to step 5 End If 2. Set del_val=q  Dque[qFront] 3.. If q  Front =q  Rear Set q  front = q  Rear = -1 Else Set qFront =q  front +1 End if 4. Return del_val 5. End
  • 20. 4.Delete operation At end of queue Procedure delete_end(q) 1. If q  Front = -1 Print “Underflow” Return 0 and go to step 5 End if 2. Set q del_val=q  Dque[qrear] 3. q  front = q  Rear Set qfront = q Rear -1 Else set q Rear = qrear-1 If qrear = -1 set qfront = -1 End if End if 4. Return del_val 5.End
  • 21.  Priority Queue: A queue in which we are able to insert items or remove items from any position based on some priority is known as priority queue. R1 R2 …… Ri-1 O1 O2 ……. Oj-1 B1 B2 …… Bk-1 1 2 …… 1 2 2 …….. 2 3 3 …… 3 : o Figure represents a priority queue of jobs waiting to use a computer o Priorities of 1, 2, 3 have been attached to jobs of real time, on-line and batch respectively. o Figure shows how the single priority queue can be visualized as three separate queues.
  • 22. o When elements are inserted, they are always added at the end of one of the queues as determined by the priorities o Elements in the second queue are deleted only when the first queue is empty. o Elements in the third queue are deleted only when the first and second queue are empty.  Operation on Priority Queue 1) Insert operation in priority Queue 2) Delete operation in priority Queue
  • 23. 1.Insert operation into priority Queue Procedure Qinsert (q,val,prno) //prno is the priority of val 1). (qRear[prno]=MAX-1 AND qFront[prno]=0) OR (qRear[prno]+1=qFront[prno]=0) Print(“overflow queue”) and goto step-5 End If 2). If qRear[prno-1]=Max-1 set qRear[prno-1]= 0 Else Set qRear[prno-1]=qRear[prno-1]+1 End if 3). Set qCqueue[prno-1] [qRear[prno-1]] =val. 4). If qFront[prno-1]= -1 Set qFront[prno-1] = 0 End if 5). End
  • 24. 2.delete operation on priority Queue Procedure Qdelete(q) 1). Set flag=0,i=0 2). while I =MAX-1 If NOT(q  Front[prno])= -1 Set flag=1 Set del_val=q  Cqueue[1][q  Front[i]] If qFront[ i ]=qRear[i] Set q  Front[i] = qRear[i]=-1 Else If q  Rear[prno-1]=Max-1 Set q  Rear[prno-1]= 0 Else Set q  Rear[prno-1]=q  Rear[prno-1]+1 End if End if Break //jump out from while loop End if Set i= i+1 End While
  • 25. 3). If FLAG=0 print(“underflow Queue”) Return 0 and go to step 4 Else Return Del_val End IF 4). End

Editor's Notes

  1. qRear[]