SlideShare a Scribd company logo
1 of 37
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-InFirst-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
FRONT: 1
REAR: 4

AA BB CC DD
1
2
3
4
5

…
6

7

N

Delete an element
FRONT: 2
REAR: 4

1

BB CC DD
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
Queue
FRONT: 2

REAR: 4

BB CC DD

1

2

3

4

…

5

6

7

N

Insert an element
FRONT: 2
REAR: 5

1

BB CC DD EE
2
3
4
5
6

…
7

Whenever an element is inserted into the
queue, the value of REAR is increased by 1
REAR = REAR + 1

N
Queue
• REAR = N and Insert an element into
queue
FRONT: 7
REAR: N

1

2

3

4

5

6

XX …
7

Move the entire queue to the beginning of the
array
Change the FRONT and REAR accordingly
Insert the element
This procedure is too expensive

ZZ
N
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
FRONT: N
REAR:

1

2

3

4

5

6

XX …
7

We reset FRONT = 1, instead of increasing
FRONT to N + 1

ZZ
N
Queue
• QUEUE contain one element
FRONT = REAR NULL
FRONT: 7
REAR: 7

1

2

3

4

5

6

FRONT = NULL and REAR = NULL

XX …
7

N
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

FRONT: 7
REAR: 6

AA BB CC DD EE FF
1
2
3
4
5
6

FRONT = REAR + 1 [FULL QUEUE]

XX …
7

ZZ
N
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
FRONT
CC

BB

AA X
REAR

15
Insertion in a Queue
FRONT
AA

BB
NEW

CC X
REAR
DD

16
Insertion in a Queue
FRONT
AA

BB

CC X
DD
REAR
17
Delete from a Queue
FRONT
CC

BB

AA X
REAR

18
Delete from a Queue

FRONT
BB

AA X
REAR

19
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: Overflow, 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
LEFT: 4
RIGHT: 7

LEFT: 4
RIGHT: 7

1

2

YY
1

3

ZZ
2
3

AA BB CC DD
4
5
6
7
8

4

5

6

WW XX
7
8
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
LEFT: 2
RIGHT: 4

1

A
2

C
3

D
4

5

6

F is added to the right
LEFT: 2
RIGHT: 5

1

A
2

C
3

D
4

F
5

6
Deque
LEFT: 2
RIGHT: 5

1

A
2

C
3

D
4

F
5

6

Two Letters on right is deleted
LEFT: 2
RIGHT: 3

1

A
2

C
3

4

5

6
Deque
LEFT: 2
RIGHT: 3

1

A
2

C
3

4

5

6

K, L and M are added to the Left
LEFT: 5
RIGHT: 3

K
1

A
2

C
3

4

M
5

L
6
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

Head
A1

B2

F2

D3

32
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
QUEUE

FRONT REAR
1
1
2
3
4
5

2
1
0
5
4

2
3
0
1
4

1
2
3
4
5

BB

2
AA
CC

3

4

5

DD

EE

DD

FF

GG
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

What's hot

Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureTushar Gonawala
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structuresDurgaDeviCbit
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure pptNalinNishant3
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in CSmit Parikh
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Muhammad Hammad Waseem
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Anand Ingle
 
What is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | EdurekaWhat is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | EdurekaEdureka!
 

What's hot (20)

Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Heaps
HeapsHeaps
Heaps
 
Quick sort
Quick sortQuick sort
Quick sort
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Queues
QueuesQueues
Queues
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
What is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | EdurekaWhat is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | Edureka
 
Arrays
ArraysArrays
Arrays
 

Similar to CS102 Data Structures Queue Priority

Similar to CS102 Data Structures Queue Priority (20)

6.queue
6.queue6.queue
6.queue
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queues
 
2.DS Array
2.DS Array2.DS Array
2.DS Array
 
Arrays
ArraysArrays
Arrays
 
Detalied information of queue
Detalied information of queueDetalied information of queue
Detalied information of queue
 
Queue
QueueQueue
Queue
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
 
Queue
QueueQueue
Queue
 
Stack q ll algo
Stack q ll algoStack q ll algo
Stack q ll algo
 
4.linked list(contd.)
4.linked list(contd.)4.linked list(contd.)
4.linked list(contd.)
 
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
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 
Data Structure and Algorithms Queues
Data Structure and Algorithms QueuesData Structure and Algorithms Queues
Data Structure and Algorithms Queues
 
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
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Data structure
Data structureData structure
Data structure
 

More from Aakash deep Singhal

Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsAakash deep Singhal
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsAakash deep Singhal
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
Lecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsLecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsAakash deep Singhal
 
Lecture 11 data structures and algorithms
Lecture 11 data structures and algorithmsLecture 11 data structures and algorithms
Lecture 11 data structures and algorithmsAakash deep Singhal
 
Lecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsLecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsAakash deep Singhal
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsAakash deep Singhal
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsAakash deep Singhal
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsAakash deep Singhal
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsAakash deep Singhal
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsAakash deep Singhal
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 
Lecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsLecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsAakash deep Singhal
 

More from Aakash deep Singhal (14)

Subsidence in coal mines
Subsidence in coal minesSubsidence in coal mines
Subsidence in coal mines
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithms
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Lecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsLecture 12 data structures and algorithms
Lecture 12 data structures and algorithms
 
Lecture 11 data structures and algorithms
Lecture 11 data structures and algorithmsLecture 11 data structures and algorithms
Lecture 11 data structures and algorithms
 
Lecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsLecture 10 data structures and algorithms
Lecture 10 data structures and algorithms
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithms
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithms
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
Lecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsLecture 16 data structures and algorithms
Lecture 16 data structures and algorithms
 

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 

Recently uploaded (20)

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 

CS102 Data Structures Queue Priority

  • 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-InFirst-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 FRONT: 1 REAR: 4 AA BB CC DD 1 2 3 4 5 … 6 7 N Delete an element FRONT: 2 REAR: 4 1 BB CC DD 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
  • 6. Queue FRONT: 2 REAR: 4 BB CC DD 1 2 3 4 … 5 6 7 N Insert an element FRONT: 2 REAR: 5 1 BB CC DD EE 2 3 4 5 6 … 7 Whenever an element is inserted into the queue, the value of REAR is increased by 1 REAR = REAR + 1 N
  • 7. Queue • REAR = N and Insert an element into queue FRONT: 7 REAR: N 1 2 3 4 5 6 XX … 7 Move the entire queue to the beginning of the array Change the FRONT and REAR accordingly Insert the element This procedure is too expensive ZZ N
  • 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 FRONT: N REAR: 1 2 3 4 5 6 XX … 7 We reset FRONT = 1, instead of increasing FRONT to N + 1 ZZ N
  • 10. Queue • QUEUE contain one element FRONT = REAR NULL FRONT: 7 REAR: 7 1 2 3 4 5 6 FRONT = NULL and REAR = NULL XX … 7 N
  • 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 FRONT: 7 REAR: 6 AA BB CC DD EE FF 1 2 3 4 5 6 FRONT = REAR + 1 [FULL QUEUE] XX … 7 ZZ N
  • 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 FRONT CC BB AA X REAR 15
  • 16. Insertion in a Queue FRONT AA BB NEW CC X REAR DD 16
  • 17. Insertion in a Queue FRONT AA BB CC X DD REAR 17
  • 18. Delete from a Queue FRONT CC BB AA X REAR 18
  • 19. Delete from a Queue FRONT BB AA X REAR 19
  • 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: Overflow, 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 LEFT: 4 RIGHT: 7 LEFT: 4 RIGHT: 7 1 2 YY 1 3 ZZ 2 3 AA BB CC DD 4 5 6 7 8 4 5 6 WW XX 7 8
  • 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 LEFT: 2 RIGHT: 4 1 A 2 C 3 D 4 5 6 F is added to the right LEFT: 2 RIGHT: 5 1 A 2 C 3 D 4 F 5 6
  • 27. Deque LEFT: 2 RIGHT: 5 1 A 2 C 3 D 4 F 5 6 Two Letters on right is deleted LEFT: 2 RIGHT: 3 1 A 2 C 3 4 5 6
  • 28. Deque LEFT: 2 RIGHT: 3 1 A 2 C 3 4 5 6 K, L and M are added to the Left LEFT: 5 RIGHT: 3 K 1 A 2 C 3 4 M 5 L 6
  • 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
  • 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
  • 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