SlideShare a Scribd company logo
Data Structure and
Algorithm (CS 102)
Ashok K Turuk
Queue
• A queue is a linear list of elements in
which
– deletion can take place only at one end
called Front, and
– Insertion takes place at one end called
Rear
• Queues are also known as First-In-
First-Out (FIFO) list
Queue
• Queue are represented in two-ways
– Linear Array
– One-way Linked List
Array representation of Queue
• A queue is maintained by a
–linear array QUEUE
– Two pointer variable
• FRONT : Containing the location of the front
element of the queue
• REAR : Containing
• FRONT == NULL indicates that the
queue is empty
Queue
AA BB CC DD …
1 2 3 4 5 6 7 N
FRONT: 2
REAR: 4
BB CC DD …
1 2 3 4 5 6 7 N
Whenever an element is deleted from the
queue, the value of FRONT is increased by 1
FRONT = FRONT + 1
FRONT: 1
REAR: 4
Delete an element
Queue
BB CC DD …
1 2 3 4 5 6 7 N
FRONT: 2
REAR: 4
Insert an element
BB CC DD EE …
1 2 3 4 5 6 7 N
FRONT: 2
REAR: 5
Whenever an element is inserted into the
queue, the value of REAR is increased by 1
REAR = REAR + 1
Queue
• REAR = N and Insert an element into
queue
XX … ZZ
1 2 3 4 5 6 7 N
FRONT: 7
REAR: N
Move the entire queue to the beginning of the
array
Change the FRONT and REAR accordingly
Insert the element
This procedure is too expensive
Queue
• Queue is assumed to be circular
• QUEUE[1] comes after QUEUE[N]
• Instead of increasing REAR to N +1, we
reset REAR = 1 and then assign
QUEUE[REAR] = ITEM
Queue
• FRONT = N and an element of QUEUE
is Deleted
XX … ZZ
1 2 3 4 5 6 7 N
FRONT: N
REAR:
We reset FRONT = 1, instead of increasing
FRONT to N + 1
Queue
• QUEUE contain one element
FRONT = REAR  NULL
XX …
1 2 3 4 5 6 7 N
FRONT: 7
REAR: 7
FRONT = NULL and REAR = NULL
Algorithm to Insert in Q
[1] If FRONT = 1 and REAR = N or if FRONT =
REAR + 1 then Print: Overflow and Exit
[2] If FRONT = NULL then
Set FRONT = 1 and REAR = 1
Else If REAR = N then
Set REAR = 1
Else
Set REAR = REAR + 1
[3] Set QUEUE[REAR] = ITEM
[4] Exit
Queue
AA BB CC DD EE FF XX … ZZ
1 2 3 4 5 6 7 N
FRONT: 7
REAR: 6
FRONT = REAR + 1 [FULL QUEUE]
Algorithm to Delete from Q
[1] If FRONT = NULL then Print: Underflow and
Exit
[2] Set ITEM = QUEUE[FRONT]
[3] If FRONT = REAR then
Set FRONT = NULL and REAR = NULL
Else If FRONT = N then
Set FRONT = 1
Else
Set FRONT = FRONT + 1
[4] Exit
Linked List Representation of Queue
• A linked queue is a queue implemented
as linked list with two pointer variable
FRONT and REAR pointing to the nodes
which is in the FRONT and REAR of the
queue
Linked List Representation of
Queue
CC XAABB
FRONT
15
REAR
Insertion in a Queue
AA XCCBB
FRONT
16
REAR
DD
NEW
Insertion in a Queue
AA XCCBB
FRONT
17
REAR
DD
Delete from a Queue
CC XAABB
FRONT
18
REAR
Delete from a Queue
XAABB
FRONT
19
REAR
Linked Queue
• No need to check for overflow condition
during insertion
• No need to view it as circular for
efficient management of space
Insertion
[1] NEW -> INFO = ITEM
NEW -> LINK = NULL
[2] If (FRONT = NULL) then
FRONT = REAR = NULL
else
Set REAR -> LINK = NEW
REAR = NEW
[3] Exit
Deletion
[1] If (FRONT = NULL) then
Print: Underflow, and Exit
[2] FRONT = FRONT -> LINK
[3] Exit
Deque
• A deque is a linear list in which
elements can be added or removed at
either end but not in the middle
• Deque is implemented by a circular
array DEQUE with pointers LEFT and
RIGHT which points to the two end of
the deque
Deque
• LEFT = NULL indicate deque is empty
AA BB CC DD
1 2 3 4 5 6 7 8
LEFT: 4
RIGHT: 7
YY ZZ WW XX
1 2 3 4 5 6 7 8
LEFT: 4
RIGHT: 7
Variation of deque
• There are two variation of deque
[1] Input-restricted queue: Deque which
allows insertions at only one end of the list
but allows deletion at both ends of the list
[2] Output-restricted queue: Deque which
allows deletion at only one end of the list
but allows insertion at both ends of the
list
Deque
A C D
1 2 3 4 5 6
LEFT: 2
RIGHT: 4
F is added to the right
A C D F
1 2 3 4 5 6
LEFT: 2
RIGHT: 5
Deque
A C D F
1 2 3 4 5 6
LEFT: 2
RIGHT: 5
Two Letters on right is deleted
A C
1 2 3 4 5 6
LEFT: 2
RIGHT: 3
Deque
A C
1 2 3 4 5 6
LEFT: 2
RIGHT: 3
K, L and M are added to the Left
K A C M L
1 2 3 4 5 6
LEFT: 5
RIGHT: 3
Priority Queue
• A priority queue is a collection of elements
such that each elements has been assigned
a priority and such that the order in which
elements are deleted and processed comes
from the following rules:
[1] Elements of higher priority is processed
before any elements of lower priority
[2] Two elements with the same priority are
processed according to the order in which
they were added to the queue
Priority Queue
• There are different ways a priority
queue can be represented such as
[1] One-way List
[2] Multiple queue
One-Way List Representation of
a Priority Queue
[1] Each node in the list will contain three
items of information: an information
field INFO, a priority number PRN,
and a link number LINK
[2] A node X precedes a node Y in the list
(a) when X has higher priority than Y or
(b) when both have same priority but X
was added to the list before Y
Queue
32
1A 2B 2F 3D
Head
Insertion and Deletion
• Deletion : Delete the first node in the
list.
• Insertion: Find the location of Insertion
Add an ITEM with priority number N
[a] Traverse the list until finding a node X
whose priority exceeds N. Insert ITEM
in front of node X
[b] If no such node is found, insert ITEM
as the last element of the list
Array representation of Priority
Queue
• Separate queue for each level of
priority
• Each queue will appear in its own
circular array and must have its own
pair of pointers, FRONT and REAR
• If each queue is given the same amount
space then a 2D queue can be used
1 2 3 4 5
1 AA
2 BB CC DD
3
4 FF DD EE
5 GG
1 2
2 1
3 0
4 5
5 4
2
3
0
1
4
FRONT REAR QUEUE
Deletion Algorithm [outline]
[1] Find the smallest K such that
FRONT[K]  NULL
[2] Delete and process the front element
in row K of QUEUE
[3] Exit
Insertion Algorithm [outline]
[1] Insert ITEM as the rear element in
row M of QUEUE
[2] Exit

More Related Content

PPT
3.6 radix sort
PDF
Quick sort
PPT
Data Structure and Algorithms Linked List
PPTX
Doubly linked list (animated)
PPTX
Linked list
PPT
Binary search tree(bst)
PPTX
Linked list
PPT
Linked list
3.6 radix sort
Quick sort
Data Structure and Algorithms Linked List
Doubly linked list (animated)
Linked list
Binary search tree(bst)
Linked list
Linked list

What's hot (20)

PPTX
Divide and conquer 1
PPTX
Doubly Linked List
PDF
Heap and heapsort
PPTX
Radix sorting
PDF
Double ended queue
PPT
3.3 shell sort
PPTX
Queue - Data Structure - Notes
PPT
Best for b trees
PPTX
Priority Queue in Data Structure
PPTX
Selection sorting
PPTX
linked list
PPTX
Binary search
PPTX
Stacks IN DATA STRUCTURES
PDF
Sorting Algorithms
PPTX
Arrays In C++
PPT
Algorithm: Quick-Sort
PPTX
Radix sort
PPT
Sorting Techniques
PPTX
Trees (data structure)
PPTX
Ppt bubble sort
Divide and conquer 1
Doubly Linked List
Heap and heapsort
Radix sorting
Double ended queue
3.3 shell sort
Queue - Data Structure - Notes
Best for b trees
Priority Queue in Data Structure
Selection sorting
linked list
Binary search
Stacks IN DATA STRUCTURES
Sorting Algorithms
Arrays In C++
Algorithm: Quick-Sort
Radix sort
Sorting Techniques
Trees (data structure)
Ppt bubble sort
Ad

Viewers also liked (14)

PPT
Listas Lineares - Parte 1
ODP
Aula 01 -_pilhas_e_filas_com_vetores-oop
PPTX
Aula01 - estrutura de dados
PPTX
Lecture 5 data structures and algorithms
PPT
Pilhas e Filas
PPTX
Mca ii dfs u-3 linklist,stack,queue
PDF
Estrutura de Dados - Listas Encadeadas
PDF
Data structure and algorithm.(dsa)
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
PDF
Estrutura de dados - Filas
PPTX
Data Structure -List Stack Queue
PDF
Estrutura de dados - Pilhas
PPTX
Estrutura de Dados - Conceitos fundamentais
PPTX
Data structures and algorithms
Listas Lineares - Parte 1
Aula 01 -_pilhas_e_filas_com_vetores-oop
Aula01 - estrutura de dados
Lecture 5 data structures and algorithms
Pilhas e Filas
Mca ii dfs u-3 linklist,stack,queue
Estrutura de Dados - Listas Encadeadas
Data structure and algorithm.(dsa)
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Estrutura de dados - Filas
Data Structure -List Stack Queue
Estrutura de dados - Pilhas
Estrutura de Dados - Conceitos fundamentais
Data structures and algorithms
Ad

Similar to 6.queue (20)

PPTX
Lecture 7 data structures and algorithms
PPTX
2.DS Array
PPTX
PPT
Unit 2 linked list and queues
PPT
queue (1).ppt queue notes and ppt in Data Structures
PPTX
Detalied information of queue
PPTX
Queue in Data Structure
PPTX
Lecture 6 data structures and algorithms
PPTX
Lecture 3 data structures and algorithms
PPT
Data Structure and Algorithms Queues
PPT
PPTX
apllicationsof queffffffffffffffffffffffffffffue.pptx
PPTX
2. Array in Data Structure
PPTX
fdfdfdsfadsfsfdsfsffggfdgdgreddsfewdcedrdfe
PPT
linked list1.ppt linked list ppts and notes
PPTX
queue_final.pptx
PPT
Lecture three of datat structures ,.The Queue-ds.ppt
PPTX
queue & its applications
DOCX
Stack q ll algo
Lecture 7 data structures and algorithms
2.DS Array
Unit 2 linked list and queues
queue (1).ppt queue notes and ppt in Data Structures
Detalied information of queue
Queue in Data Structure
Lecture 6 data structures and algorithms
Lecture 3 data structures and algorithms
Data Structure and Algorithms Queues
apllicationsof queffffffffffffffffffffffffffffue.pptx
2. Array in Data Structure
fdfdfdsfadsfsfdsfsffggfdgdgreddsfewdcedrdfe
linked list1.ppt linked list ppts and notes
queue_final.pptx
Lecture three of datat structures ,.The Queue-ds.ppt
queue & its applications
Stack q ll algo

More from Chandan Singh (20)

PDF
Fundamental of Tissue engineering
PPT
Resistance Measurement instruments
PDF
Resistance Measurement instruments
PDF
Moving iron (MI) instruments
PPT
Moving iron (MI) instruments
PPT
Electrical Measurement & Instruments
PPT
Static characteristics of Instruments
PPT
Resistance measurement
PPT
Introduction to sensors
PPT
Energy meter
PPT
Classification (Analog instruments)
PPT
AC Bridges: Balance Condition
PPT
Cathode Ray Osciloscope
PPT
Instrument transformer CT & PT
PPT
Megohmmeter
PPT
Moving Iron
PPT
Permanent Magnet Moving Coil
PPTX
10.m way search tree
PPTX
9.bst(contd.) avl tree
PPTX
8.binry search tree
Fundamental of Tissue engineering
Resistance Measurement instruments
Resistance Measurement instruments
Moving iron (MI) instruments
Moving iron (MI) instruments
Electrical Measurement & Instruments
Static characteristics of Instruments
Resistance measurement
Introduction to sensors
Energy meter
Classification (Analog instruments)
AC Bridges: Balance Condition
Cathode Ray Osciloscope
Instrument transformer CT & PT
Megohmmeter
Moving Iron
Permanent Magnet Moving Coil
10.m way search tree
9.bst(contd.) avl tree
8.binry search tree

Recently uploaded (20)

PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Geodesy 1.pptx...............................................
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Construction Project Organization Group 2.pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
additive manufacturing of ss316l using mig welding
PDF
PPT on Performance Review to get promotions
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Well-logging-methods_new................
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
web development for engineering and engineering
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Embodied AI: Ushering in the Next Era of Intelligent Systems
Geodesy 1.pptx...............................................
Lecture Notes Electrical Wiring System Components
Construction Project Organization Group 2.pptx
Mechanical Engineering MATERIALS Selection
additive manufacturing of ss316l using mig welding
PPT on Performance Review to get promotions
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Digital Logic Computer Design lecture notes
Foundation to blockchain - A guide to Blockchain Tech
Well-logging-methods_new................
OOP with Java - Java Introduction (Basics)
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Internet of Things (IOT) - A guide to understanding
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
web development for engineering and engineering
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS

6.queue

  • 1. Data Structure and Algorithm (CS 102) Ashok K Turuk
  • 2. Queue • A queue is a linear list of elements in which – deletion can take place only at one end called Front, and – Insertion takes place at one end called Rear • Queues are also known as First-In- First-Out (FIFO) list
  • 3. Queue • Queue are represented in two-ways – Linear Array – One-way Linked List
  • 4. Array representation of Queue • A queue is maintained by a –linear array QUEUE – Two pointer variable • FRONT : Containing the location of the front element of the queue • REAR : Containing • FRONT == NULL indicates that the queue is empty
  • 5. Queue AA BB CC DD … 1 2 3 4 5 6 7 N FRONT: 2 REAR: 4 BB CC DD … 1 2 3 4 5 6 7 N Whenever an element is deleted from the queue, the value of FRONT is increased by 1 FRONT = FRONT + 1 FRONT: 1 REAR: 4 Delete an element
  • 6. Queue BB CC DD … 1 2 3 4 5 6 7 N FRONT: 2 REAR: 4 Insert an element BB CC DD EE … 1 2 3 4 5 6 7 N FRONT: 2 REAR: 5 Whenever an element is inserted into the queue, the value of REAR is increased by 1 REAR = REAR + 1
  • 7. Queue • REAR = N and Insert an element into queue XX … ZZ 1 2 3 4 5 6 7 N FRONT: 7 REAR: N Move the entire queue to the beginning of the array Change the FRONT and REAR accordingly Insert the element This procedure is too expensive
  • 8. Queue • Queue is assumed to be circular • QUEUE[1] comes after QUEUE[N] • Instead of increasing REAR to N +1, we reset REAR = 1 and then assign QUEUE[REAR] = ITEM
  • 9. Queue • FRONT = N and an element of QUEUE is Deleted XX … ZZ 1 2 3 4 5 6 7 N FRONT: N REAR: We reset FRONT = 1, instead of increasing FRONT to N + 1
  • 10. Queue • QUEUE contain one element FRONT = REAR  NULL XX … 1 2 3 4 5 6 7 N FRONT: 7 REAR: 7 FRONT = NULL and REAR = NULL
  • 11. Algorithm to Insert in Q [1] If FRONT = 1 and REAR = N or if FRONT = REAR + 1 then Print: Overflow and Exit [2] If FRONT = NULL then Set FRONT = 1 and REAR = 1 Else If REAR = N then Set REAR = 1 Else Set REAR = REAR + 1 [3] Set QUEUE[REAR] = ITEM [4] Exit
  • 12. Queue AA BB CC DD EE FF XX … ZZ 1 2 3 4 5 6 7 N FRONT: 7 REAR: 6 FRONT = REAR + 1 [FULL QUEUE]
  • 13. Algorithm to Delete from Q [1] If FRONT = NULL then Print: Underflow and Exit [2] Set ITEM = QUEUE[FRONT] [3] If FRONT = REAR then Set FRONT = NULL and REAR = NULL Else If FRONT = N then Set FRONT = 1 Else Set FRONT = FRONT + 1 [4] Exit
  • 14. Linked List Representation of Queue • A linked queue is a queue implemented as linked list with two pointer variable FRONT and REAR pointing to the nodes which is in the FRONT and REAR of the queue
  • 15. Linked List Representation of Queue CC XAABB FRONT 15 REAR
  • 16. Insertion in a Queue AA XCCBB FRONT 16 REAR DD NEW
  • 17. Insertion in a Queue AA XCCBB FRONT 17 REAR DD
  • 18. Delete from a Queue CC XAABB FRONT 18 REAR
  • 19. Delete from a Queue XAABB FRONT 19 REAR
  • 20. Linked Queue • No need to check for overflow condition during insertion • No need to view it as circular for efficient management of space
  • 21. Insertion [1] NEW -> INFO = ITEM NEW -> LINK = NULL [2] If (FRONT = NULL) then FRONT = REAR = NULL else Set REAR -> LINK = NEW REAR = NEW [3] Exit
  • 22. Deletion [1] If (FRONT = NULL) then Print: Underflow, and Exit [2] FRONT = FRONT -> LINK [3] Exit
  • 23. Deque • A deque is a linear list in which elements can be added or removed at either end but not in the middle • Deque is implemented by a circular array DEQUE with pointers LEFT and RIGHT which points to the two end of the deque
  • 24. Deque • LEFT = NULL indicate deque is empty AA BB CC DD 1 2 3 4 5 6 7 8 LEFT: 4 RIGHT: 7 YY ZZ WW XX 1 2 3 4 5 6 7 8 LEFT: 4 RIGHT: 7
  • 25. Variation of deque • There are two variation of deque [1] Input-restricted queue: Deque which allows insertions at only one end of the list but allows deletion at both ends of the list [2] Output-restricted queue: Deque which allows deletion at only one end of the list but allows insertion at both ends of the list
  • 26. Deque A C D 1 2 3 4 5 6 LEFT: 2 RIGHT: 4 F is added to the right A C D F 1 2 3 4 5 6 LEFT: 2 RIGHT: 5
  • 27. Deque A C D F 1 2 3 4 5 6 LEFT: 2 RIGHT: 5 Two Letters on right is deleted A C 1 2 3 4 5 6 LEFT: 2 RIGHT: 3
  • 28. Deque A C 1 2 3 4 5 6 LEFT: 2 RIGHT: 3 K, L and M are added to the Left K A C M L 1 2 3 4 5 6 LEFT: 5 RIGHT: 3
  • 29. Priority Queue • A priority queue is a collection of elements such that each elements has been assigned a priority and such that the order in which elements are deleted and processed comes from the following rules: [1] Elements of higher priority is processed before any elements of lower priority [2] Two elements with the same priority are processed according to the order in which they were added to the queue
  • 30. Priority Queue • There are different ways a priority queue can be represented such as [1] One-way List [2] Multiple queue
  • 31. One-Way List Representation of a Priority Queue [1] Each node in the list will contain three items of information: an information field INFO, a priority number PRN, and a link number LINK [2] A node X precedes a node Y in the list (a) when X has higher priority than Y or (b) when both have same priority but X was added to the list before Y
  • 32. Queue 32 1A 2B 2F 3D Head
  • 33. Insertion and Deletion • Deletion : Delete the first node in the list. • Insertion: Find the location of Insertion Add an ITEM with priority number N [a] Traverse the list until finding a node X whose priority exceeds N. Insert ITEM in front of node X [b] If no such node is found, insert ITEM as the last element of the list
  • 34. Array representation of Priority Queue • Separate queue for each level of priority • Each queue will appear in its own circular array and must have its own pair of pointers, FRONT and REAR • If each queue is given the same amount space then a 2D queue can be used
  • 35. 1 2 3 4 5 1 AA 2 BB CC DD 3 4 FF DD EE 5 GG 1 2 2 1 3 0 4 5 5 4 2 3 0 1 4 FRONT REAR QUEUE
  • 36. Deletion Algorithm [outline] [1] Find the smallest K such that FRONT[K]  NULL [2] Delete and process the front element in row K of QUEUE [3] Exit
  • 37. Insertion Algorithm [outline] [1] Insert ITEM as the rear element in row M of QUEUE [2] Exit