SlideShare a Scribd company logo
1 of 24
Queue
6-2
Introduction
Queue: a collection whose elements are added at
one end (the rear or tail of the queue) and
removed from the other end (the front or head of
the queue)
A queue is a FIFO (first in, first out) data structure
6-3
Conceptual View of a
Queue
Front of queue
Adding an element
New element is added to
the rear of the queue
6-4
Conceptual View of a
QueueRemoving an element
Element is removed from
the front of the queue
Components of Queue
• Front is a variable which refers to first position in
queue.
• Rear is a variable which refers to last position in
queue.
MaxQueue is variable that
describes
maximum number of elements in a queue.
6-6
Uses of Queues in Computing
For any kind of problem involving FIFO data
Printer queue
Keyboard input buffer
Queue Operation
Two basic operations are associated with queue:
1.“Insert” operation is used to insert an element into a
queue. (From the rear)
2. 2. “Delete” operation is used to delete an element
from a queue. (From the Front)
Steps in Insertion operation:
•Queue can be added when it’s not full
•If queue is empty then front and rear is added
by 1. For the contrary, rear is added by 1.
Insertion Operation
Insertion
front rear
Front=0 Rear=0
queue is empty
Front=1 Rear=1
value=3
Front=1 Rear=2
Value=2
Queue
0 1 2 3 4
3 2
Algorithm of Insertion
Algorithm-Insertion
Write an algorithm to add an “item“ in a queue ‘Q’.
1.IF R=N THEN
Print “Overflow”
Return
Else
R=R+1 [End of IF Structure]
2.[Insert Item]
Q[R]=“item”
IF F=0 THEN
F=1
END IF
Steps in deletion operation:
•Queue of element can be deleted when its
elements is not empty.
•When the element is deleted from the queue
then subtract the rear pointer with 1.
Deletion
Algorithm for deletion
Algorithm-Insertion
Write an algorithm to add an “item” in a queue.
Step 1: If FRONT = 0 then
Write “Queue is Underflow”           
Step 2: Delete QUEUE [FRONT]
Step 3: If FRONT = REAR then
FRONT = 0           
REAR = 0           
Else           
FRONT = FRONT + 1           
Step 4: Exit
3
Figure: Circular Queue having
Rear = 5 and Front = 0
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:
•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.
Example: Consider the following circular queue with N = 5.
1. Initially, Rear = 0, Front =
0.
3. Insert 50, Rear = 2, Front = 1.
Rear
14
Rear
2. Insert 10, Rear = 1, Front = 1.
Rear Front
Front
4. Insert 20, Rear = 3, Front = 1.
Front
Rear
5. Insert 70, Rear = 4, Front = 1.
Front
Rear
6. Delete front, Rear = 4,
Front = 2.
Front
7. Insert 100, Rear = 5, Front = 2.
9. Insert 140, Rear = 1, Front = 2.
As Front = Rear + 1, so Queue overflow.
Front
Rear
8. Insert 40, Rear = 1, Front =
2.
Rear Front
Rear
10. Delete front, Rear = 1, Front = 3.
Rear
Front
Front
11. Delete
front, Rear =
1, Front = 4.
Rear
15
Front
12. Delete front, Rear = 1,
Front = 5.
Rear
Front
Algorithm Insertion For Circular Queue
Algorithm-insertioncq
The algorithm inserts an element X into queue ‘Q’ having N elements. F and R
represent the pointers to the front and Rear element of the queue.
1.[Check Overflow]
IF (F=1 and R=N ) or (F=R+1)THEN
Print “Overflow”
Return [End of IF Structrue]
2.IF (F=0 and R=0) THEN
F=1
R=1
Else IF R=N THEN
R=1
Else R=R+1 [End of IF Structure]
Q[R]=X [Insert Element X]
3. Exit
Algorithm Deletion For Circular Queue
Algorithm-Deletioncq
The algorithm delete an element X into queue ‘Q’ having N
elements. F and R represent the pointers to the front and Rear
element of the queue.
1.[Check whether or not The Queue is empty]
IF F=0 THEN
Print “Queue is Empty”
Return [End of IF Structrue]
2. [Delete Element]
Delete Q[F]
3. IF F=R THEN
F=R=0
Else IF F=N THEN
F=1
Else F=F+1 [End of IF Structure]
3. Exit
Double Ended Queue
The deque is pronounced as de-queue. It is a linear structure
in which items can be added or removed at either end.
However, an item cannot be added or deleted in the middle
of the deque. Since items can be added or deleted at both
ends of the queue.so it is called double ended queue. The
deque is represented as shown in the following figure
Types of Deque
There are two types of deque depending upon the restriction
to perform insertion or deletion operations at the two ends.
1. Input restricted deque
An input restricted deque is a deque, which allows
insertion at only 1 end, rear end, but allows deletion at
both ends, rear and front end of the lists
2. Output restricted deque
An output-restricted deque is a deque, which
allows deletion at only one end, front end, but allows
insertion at both ends, rear and front ends, of the lists.
The possible operation performed on
deque
1.Add an element at the rear end
2.Add an element at the front end
3. Delete an element from the front end
4. Delete an element from the rear end
Only 1st
, 3rd
and 4th
operations are performed by input-restricted
deque
1st, 2nd
and 3rd
operations are performed by output-restricted
deque.
20
Algorithm For insertion at rear end in Deque
Algorithm-insertiondq
The algorithm is used to inserts an element “item” into queue ‘DQ’ having N
elements. F and R represent the pointers to the front and Rear element of the
queue.
1.[Check Overflow]
IF F=1 and R=N THEN
Print “Overflow”
Return [End of IF Structrue]
2.IF (F=0 and R=0) THEN
F=1
R=1
Else IF R=N THEN
R=1
Else R=R+1 [End of IF Structure]
DQ[R]=X [Insert Element X]
3. Exit
Algorithm For insertion at Front end in Deque
Algorithm-insertiondq
The algorithm is used to inserts an element “item” into queue ‘Q’ having N
elements from front end. F and R represent the pointers to the front and Rear
element of the queue.
1.[Check Overflow]
IF F=1 and R=N THEN
Print “Overflow”
Return [End of IF Structrue]
2.IF (F=0) THEN
F=1
R=1
Else IF (F=1) THEN
F=N
Else F=F-1 [End of IF Structure]
DQ[F]=X [Insert Element X]
3. Exit
Algorithm For deletion from Front end in Deque
Algorithm-deletiondqfront
The algorithm is used to inserts an element “item” into queue ‘DQ’ having N
elements from front end. F and R represent the pointers to the front and Rear
element of the queue.
1.[Check Overflow]
IF F=0 THEN
Print “underflow”
Return [End of IF Structrue]
2. Delete DQ[F]
3. IF (F=R) THEN
F=0
R=0
Else IF (F=N) THEN
F=1
Else F=F+1 [End of IF Structure]
3. Exit
Algorithm For deletion from rear end in Deque
Algorithm-deletiondqrear
The algorithm is used to inserts an element “item” into queue ‘DQ’ having N
elements from front end. F and R represent the pointers to the front and Rear
element of the queue.
1.[Check Overflow]
IF R=0 THEN
Print “underflow”
Return [End of IF Structrue]
2. Delete DQ[R]
3. IF (R=F) THEN
F=0
R=0
Else IF (R=1) THEN
R=N
Else R=R-1 [End of IF Structure]
3. Exit

More Related Content

What's hot

Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applicationsJsaddam Hussain
 
Detalied information of queue
Detalied information of queueDetalied information of queue
Detalied information of queueSmit Parikh
 
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 Structure
Queue-Data StructureQueue-Data Structure
Queue-Data StructurePaurav Shah
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - NotesOmprakash Chauhan
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queuezzzubair
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - NotesOmprakash Chauhan
 
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 QueueBalwant Gorad
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Roman Rodomansky
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structureeShikshak
 

What's hot (20)

Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Stack
StackStack
Stack
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Detalied information of queue
Detalied information of queueDetalied information of queue
Detalied information of 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 Structure
Queue-Data StructureQueue-Data Structure
Queue-Data Structure
 
Queues-handouts
Queues-handoutsQueues-handouts
Queues-handouts
 
Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queue
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
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
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Queue
QueueQueue
Queue
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Circular queue
Circular queueCircular queue
Circular queue
 
Team 6
Team 6Team 6
Team 6
 

Similar to Queue

4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data StructureMandeep Singh
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptxNuraMohamed9
 
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,queueRai University
 
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,queueRai University
 
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,queueRai University
 
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
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptxsinghprpg
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptxSonaPathak4
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 

Similar to Queue (20)

Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
05 queues
05 queues05 queues
05 queues
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
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
 
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
 
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 & ITS TYPES
Queues & ITS TYPESQueues & ITS TYPES
Queues & ITS TYPES
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Queue
QueueQueue
Queue
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 

Recently uploaded

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 

Recently uploaded (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 

Queue

  • 2. 6-2 Introduction Queue: a collection whose elements are added at one end (the rear or tail of the queue) and removed from the other end (the front or head of the queue) A queue is a FIFO (first in, first out) data structure
  • 3. 6-3 Conceptual View of a Queue Front of queue Adding an element New element is added to the rear of the queue
  • 4. 6-4 Conceptual View of a QueueRemoving an element Element is removed from the front of the queue
  • 5. Components of Queue • Front is a variable which refers to first position in queue. • Rear is a variable which refers to last position in queue. MaxQueue is variable that describes maximum number of elements in a queue.
  • 6. 6-6 Uses of Queues in Computing For any kind of problem involving FIFO data Printer queue Keyboard input buffer
  • 7. Queue Operation Two basic operations are associated with queue: 1.“Insert” operation is used to insert an element into a queue. (From the rear) 2. 2. “Delete” operation is used to delete an element from a queue. (From the Front)
  • 8. Steps in Insertion operation: •Queue can be added when it’s not full •If queue is empty then front and rear is added by 1. For the contrary, rear is added by 1. Insertion Operation
  • 9. Insertion front rear Front=0 Rear=0 queue is empty Front=1 Rear=1 value=3 Front=1 Rear=2 Value=2 Queue 0 1 2 3 4 3 2
  • 10. Algorithm of Insertion Algorithm-Insertion Write an algorithm to add an “item“ in a queue ‘Q’. 1.IF R=N THEN Print “Overflow” Return Else R=R+1 [End of IF Structure] 2.[Insert Item] Q[R]=“item” IF F=0 THEN F=1 END IF
  • 11. Steps in deletion operation: •Queue of element can be deleted when its elements is not empty. •When the element is deleted from the queue then subtract the rear pointer with 1. Deletion
  • 12. Algorithm for deletion Algorithm-Insertion Write an algorithm to add an “item” in a queue. Step 1: If FRONT = 0 then Write “Queue is Underflow”            Step 2: Delete QUEUE [FRONT] Step 3: If FRONT = REAR then FRONT = 0            REAR = 0            Else            FRONT = FRONT + 1            Step 4: Exit
  • 13. 3 Figure: Circular Queue having Rear = 5 and Front = 0 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: •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.
  • 14. Example: Consider the following circular queue with N = 5. 1. Initially, Rear = 0, Front = 0. 3. Insert 50, Rear = 2, Front = 1. Rear 14 Rear 2. Insert 10, Rear = 1, Front = 1. Rear Front Front 4. Insert 20, Rear = 3, Front = 1. Front Rear 5. Insert 70, Rear = 4, Front = 1. Front Rear 6. Delete front, Rear = 4, Front = 2. Front
  • 15. 7. Insert 100, Rear = 5, Front = 2. 9. Insert 140, Rear = 1, Front = 2. As Front = Rear + 1, so Queue overflow. Front Rear 8. Insert 40, Rear = 1, Front = 2. Rear Front Rear 10. Delete front, Rear = 1, Front = 3. Rear Front Front 11. Delete front, Rear = 1, Front = 4. Rear 15 Front 12. Delete front, Rear = 1, Front = 5. Rear Front
  • 16. Algorithm Insertion For Circular Queue Algorithm-insertioncq The algorithm inserts an element X into queue ‘Q’ having N elements. F and R represent the pointers to the front and Rear element of the queue. 1.[Check Overflow] IF (F=1 and R=N ) or (F=R+1)THEN Print “Overflow” Return [End of IF Structrue] 2.IF (F=0 and R=0) THEN F=1 R=1 Else IF R=N THEN R=1 Else R=R+1 [End of IF Structure] Q[R]=X [Insert Element X] 3. Exit
  • 17. Algorithm Deletion For Circular Queue Algorithm-Deletioncq The algorithm delete an element X into queue ‘Q’ having N elements. F and R represent the pointers to the front and Rear element of the queue. 1.[Check whether or not The Queue is empty] IF F=0 THEN Print “Queue is Empty” Return [End of IF Structrue] 2. [Delete Element] Delete Q[F] 3. IF F=R THEN F=R=0 Else IF F=N THEN F=1 Else F=F+1 [End of IF Structure] 3. Exit
  • 18. Double Ended Queue The deque is pronounced as de-queue. It is a linear structure in which items can be added or removed at either end. However, an item cannot be added or deleted in the middle of the deque. Since items can be added or deleted at both ends of the queue.so it is called double ended queue. The deque is represented as shown in the following figure
  • 19. Types of Deque There are two types of deque depending upon the restriction to perform insertion or deletion operations at the two ends. 1. Input restricted deque An input restricted deque is a deque, which allows insertion at only 1 end, rear end, but allows deletion at both ends, rear and front end of the lists 2. Output restricted deque An output-restricted deque is a deque, which allows deletion at only one end, front end, but allows insertion at both ends, rear and front ends, of the lists.
  • 20. The possible operation performed on deque 1.Add an element at the rear end 2.Add an element at the front end 3. Delete an element from the front end 4. Delete an element from the rear end Only 1st , 3rd and 4th operations are performed by input-restricted deque 1st, 2nd and 3rd operations are performed by output-restricted deque. 20
  • 21. Algorithm For insertion at rear end in Deque Algorithm-insertiondq The algorithm is used to inserts an element “item” into queue ‘DQ’ having N elements. F and R represent the pointers to the front and Rear element of the queue. 1.[Check Overflow] IF F=1 and R=N THEN Print “Overflow” Return [End of IF Structrue] 2.IF (F=0 and R=0) THEN F=1 R=1 Else IF R=N THEN R=1 Else R=R+1 [End of IF Structure] DQ[R]=X [Insert Element X] 3. Exit
  • 22. Algorithm For insertion at Front end in Deque Algorithm-insertiondq The algorithm is used to inserts an element “item” into queue ‘Q’ having N elements from front end. F and R represent the pointers to the front and Rear element of the queue. 1.[Check Overflow] IF F=1 and R=N THEN Print “Overflow” Return [End of IF Structrue] 2.IF (F=0) THEN F=1 R=1 Else IF (F=1) THEN F=N Else F=F-1 [End of IF Structure] DQ[F]=X [Insert Element X] 3. Exit
  • 23. Algorithm For deletion from Front end in Deque Algorithm-deletiondqfront The algorithm is used to inserts an element “item” into queue ‘DQ’ having N elements from front end. F and R represent the pointers to the front and Rear element of the queue. 1.[Check Overflow] IF F=0 THEN Print “underflow” Return [End of IF Structrue] 2. Delete DQ[F] 3. IF (F=R) THEN F=0 R=0 Else IF (F=N) THEN F=1 Else F=F+1 [End of IF Structure] 3. Exit
  • 24. Algorithm For deletion from rear end in Deque Algorithm-deletiondqrear The algorithm is used to inserts an element “item” into queue ‘DQ’ having N elements from front end. F and R represent the pointers to the front and Rear element of the queue. 1.[Check Overflow] IF R=0 THEN Print “underflow” Return [End of IF Structrue] 2. Delete DQ[R] 3. IF (R=F) THEN F=0 R=0 Else IF (R=1) THEN R=N Else R=R-1 [End of IF Structure] 3. Exit