SlideShare a Scribd company logo
Circular Queue & Priority Queue

1
Queue (Linear Queue)
• It is a linear data structure consisting of list of items.
• In queue, data elements are added at one end, called the rear and removed from another
end, called the front of the list.
• Two basic operations are associated with queue:

7

1. “Insert” operation is used to insert an element into a queue.
2. “Delete” operation is used to delete an element from a queue.
●

6

FIFO list

EEE

1

Queue: AAA, BBB, CCC, DDD, EEE

AAA

2

BBB

3

4

CCC

DDD

5

EEE

6

7

5

DDD

4

CCC

3

BBB

• Example:

2

AAA

1

Front

Rear
09/10/08

Rear
Front
2
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

Figure: Circular Queue having
Rear = 5 and Front = 0

a "Queue overflow" state.
3
Algorithms for Insert and Delete Operations in Circular Queue
For Insert Operation
Insert-Circular-Q(CQueue, Rear, Front, N, Item)

CQueue is a circular queue where to store data.
Rear represents the location in which the data element is to be inserted and
Front represents the location from which the data element is to be removed.
Here N is the maximum size of CQueue and finally, Item is the new item to be

added.
Initailly Rear = 0 and Front = 0.
1. If Front = 0 and Rear = 0 then Set Front := 1 and go to step 4.
2. If Front =1 and Rear = N or Front = Rear + 1
then Print: “Circular Queue Overflow” and Return.
3. If Rear = N then Set Rear := 1 and go to step 5.
4. Set Rear := Rear + 1
5. Set CQueue [Rear] := Item.
6. Return

4
For Delete Operation
Delete-Circular-Q(CQueue, Front, Rear, Item)
Here, CQueue is the place where data are stored. Rear represents the location in
which the data element is to be inserted and Front represents the location from

which the data element is to be removed. Front element is assigned to Item.
Initially, Front = 1.
1. If Front = 0 then
Print: “Circular Queue Underflow” and Return.

/*..Delete without Insertion

2. Set Item := CQueue [Front]
3. If Front = N then Set Front = 1 and Return.
4. If Front = Rear then Set Front = 0 and Rear = 0 and Return.
5. Set Front := Front + 1
6. Return.

5
Example: Consider the following circular queue with N = 5.

1. Initially, Rear = 0, Front = 0.

4. Insert 20, Rear = 3, Front = 0.
Front

Rear

2. Insert 10, Rear = 1, Front = 1.
Rear
Front

5. Insert 70, Rear = 4, Front = 1.
Front

Rear

3. Insert 50, Rear = 2, Front = 1.
Front

6. Delete front, Rear = 4, Front = 2.
Front

Rear

Rear
6
7. Insert 100, Rear = 5, Front = 2.
Front

10. Delete front, Rear = 1, Front = 3.
Rear

Front
Rear

8. Insert 40, Rear = 1, Front = 2.

11. Delete front, Rear = 1, Front = 4.
Rear

Rear

Front

Front

12. Delete front, Rear = 1, Front = 5.
9. Insert 140, Rear = 1, Front = 2.
Rear
As Front = Rear + 1, so Queue overflow.
Rear

Front
Front

7
Insertion
qfull() {
if(front==(rear+1)%size)
return 1;
else
return 0;
}

int insert(int item)
{
if(qfull())
printf("circular q is full");
else
{
if(front==-1)
front=rear=0;
else
rear=(rear+1)%size;
que[rear]=item;
}
}
Deletion
int qempty()
{
if(front==-1)
return 1;
else
return 0;
}
int delete()
{
int item;
if(qempty())
printf("queue is empty");
else
{
item=que[front];
if(front==rear) {
front=rear=-1;
}
else
front=(front+1)%size;
printf("the deleted item is%d ",item);
}
Display
void display()
{
int i;
if(qempty())
{
printf("queue is empty");
return;
}
i=front;
while(i!=rear)
{
printf(“%d”,que[i]);
i=(i+1)%size;
}
Printf(“%d”,que[i]);
}
Priority Queue
Priority Queue

A Priority Queue – a different kind of queue.
Similar to a regular queue:
insert in rear,
remove from front.

Items in priority queue are ordered by some key
Item with the lowest key / highest key is always at the front
from where they are removed.
Items then „inserted‟ in „proper‟ position
Idea behind the Priority Queue is simple:
Is a queue
But the items are ordered by a key.
Implies your ‘position’ in the queue may be changed by
the arrival of a new item.
12
Applications of Priority Queues
Many, many applications.
Scheduling queues for a processor, print queues, transmit
queues, backlogs, etc.….

This means this item will be in the front of queue
“Obtained” via a remove().
Note: a priority queue is no longer FIFO!
You will still remove from front of queue, but
insertions are governed by a priority.
13
Priority Queues: Access
remove()
So, the first item has priority and can be retrieved (removed) quickly and
returned to calling environment.
Hence, „remove()‟ is easy (and will take O(1) time)

insert()
But, we want to insert quickly. Must go into proper position.
For our purposes here: Implementing data structure: array;
• slow to insert(), but for
small number of items in the pqueue, and
where insertion speed is not critical,

• this is the simplest and best approach.

14
Priority Queues
Operations performed on priority queues
1) Find an element,
2) Insert a new element

3) Delete an element, etc.
Two kinds of (Min, Max) priority queues exist:
• In a Min priority queue, find/delete operation finds/deletes the
element with minimum priority

• In a Max priority queue, find/delete operation finds/deletes the
element with maximum priority
• Two or more elements can have the same priority

15
Implementation of Priority Queues
Implemented using heaps and leftist trees
• Heap is a complete binary tree that is efficiently stored using
the array-based representation
• Leftist tree is a linked data structure suitable for the
implementation of a priority queue

16
Heap Operations
When n is the number of elements (heap size),
• Insertion  O(log2n)
• Deletion  O(log2n)
• Initialization  O(n)

17
Insertion into a Max Heap
9

8

7

6

5

7

1

5

2

6

• New element is 5
• Are we finished?

18
Insertion into a Max Heap
9

8

7

6

5

7

1

20

2

6

• New element is 20
• Are we finished?

19
Insertion into a Max Heap
9

8

7

6

5

20

1

7

2

6
• Exchange the positions with 7
• Are we finished?

20
Insertion into a Max Heap
9

20

7

6

5

8

1

7

2

6
• Exchange the positions with 8
• Are we finished?

21
Insertion into a Max Heap
20

9

7

6

5

8

1

7

2

6
• Exchange the positions with 9
• Are we finished?

22
Complexity of Insertion
At each level, we do (1) work
Thus the time complexity is O(height) =
O(log2n), where n is the heap size

23
Deletion from a Max Heap
20

15

7

6

5

9

1

7

2

8

6

• Max element is in the root
• What happens when we
delete an element?

24
Deletion from a Max Heap
15

7

6

5

9

1

7

2

8

6

• After the max element is
removed.
• Are we finished?

25
Deletion from a Max Heap
15

7

6

5

9

1

7

2

8

6

• Heap with 10 nodes.
• Reinsert 8 into the heap.

26
Deletion from a Max Heap
8

15

7

6

5

9

1

7

2

6

• Reinsert 8 into the heap.
• Are we finished?

27
Deletion from a Max Heap
15

8

7

6

5

9

1

7

2

6

• Exchange the position with 15
• Are we finished?

28
Deletion from a Max Heap
15

9

7

6

5

8

1

7

2

6

• Exchange the position with 9
• Are we finished?

29
Complexity of Deletion
• The time complexity of deletion is the same as
insertion
• At each level, we do (1) work
• Thus the time complexity is O(height) =
O(log2n), where n is the heap size

30
END!!!

31

More Related Content

What's hot (20)

PDF
linked lists in data structures
DurgaDeviCbit
 
PPT
Stacks
sweta dargad
 
PDF
Stack
Zaid Shabbir
 
PPTX
Circular Queue data structure
Dhananjaysinh Jhala
 
PPTX
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
PPTX
Ppt on Linked list,stack,queue
Srajan Shukla
 
PPTX
sorting and its types
SIVASHANKARIRAJAN
 
PPTX
Queue - Data Structure - Notes
Omprakash Chauhan
 
PDF
Dsa circular queue
zzzubair
 
PPTX
Unit I-Data structures stack & Queue
DrkhanchanaR
 
PDF
Linear search algorithm
NeoClassical
 
PPT
Stack
srihariyenduri
 
PPTX
Data Structures (CS8391)
Elavarasi K
 
PPTX
Circular queue
Lovely Professional University
 
PPTX
Presentation on queue
Rojan Pariyar
 
PPT
1.5 binary search tree
Krish_ver2
 
PPTX
Insertion sort
Monalisa Patel
 
PPTX
Quick Sort
Shweta Sahu
 
PDF
Queue as data_structure
eShikshak
 
PDF
Array data structure
maamir farooq
 
linked lists in data structures
DurgaDeviCbit
 
Stacks
sweta dargad
 
Circular Queue data structure
Dhananjaysinh Jhala
 
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Ppt on Linked list,stack,queue
Srajan Shukla
 
sorting and its types
SIVASHANKARIRAJAN
 
Queue - Data Structure - Notes
Omprakash Chauhan
 
Dsa circular queue
zzzubair
 
Unit I-Data structures stack & Queue
DrkhanchanaR
 
Linear search algorithm
NeoClassical
 
Data Structures (CS8391)
Elavarasi K
 
Presentation on queue
Rojan Pariyar
 
1.5 binary search tree
Krish_ver2
 
Insertion sort
Monalisa Patel
 
Quick Sort
Shweta Sahu
 
Queue as data_structure
eShikshak
 
Array data structure
maamir farooq
 

Viewers also liked (20)

PPT
Queue data structure
anooppjoseph
 
PPT
Queue Data Structure
Zidny Nafan
 
PPT
Circular queues
Ssankett Negi
 
PPT
Algorithm: priority queue
Tareq Hasan
 
PPTX
Ppt presentation of queues
Buxoo Abdullah
 
PPTX
deque and it applications
Sathasivam Rangasamy
 
PPT
Priority queues
Yeela Mehroz
 
PPTX
Queue Data Structure (w/ php egs)
Roman Rodomansky
 
PPT
Notes DATA STRUCTURE - queue
Farhanum Aziera
 
PPTX
Priority queue
Alvian yudha Prawira
 
PPTX
Applications of queues ii
Tech_MX
 
PPT
23 priority queue
Godo Dodo
 
PPT
Circular linked list
dchuynh
 
PPTX
queue & its applications
somendra kumar
 
PPSX
Stack
Seema Sharma
 
PPTX
STACKS IN DATASTRUCTURE
Archie Jamwal
 
PDF
computer notes - Priority queue
ecomputernotes
 
PPT
Heaps & priority queues
Pedro Hugo Valencia Morales
 
PDF
circular linked list
Materi Kuliah Online
 
DOCX
Conversion from infix to prefix using stack
Haqnawaz Ch
 
Queue data structure
anooppjoseph
 
Queue Data Structure
Zidny Nafan
 
Circular queues
Ssankett Negi
 
Algorithm: priority queue
Tareq Hasan
 
Ppt presentation of queues
Buxoo Abdullah
 
deque and it applications
Sathasivam Rangasamy
 
Priority queues
Yeela Mehroz
 
Queue Data Structure (w/ php egs)
Roman Rodomansky
 
Notes DATA STRUCTURE - queue
Farhanum Aziera
 
Priority queue
Alvian yudha Prawira
 
Applications of queues ii
Tech_MX
 
23 priority queue
Godo Dodo
 
Circular linked list
dchuynh
 
queue & its applications
somendra kumar
 
STACKS IN DATASTRUCTURE
Archie Jamwal
 
computer notes - Priority queue
ecomputernotes
 
Heaps & priority queues
Pedro Hugo Valencia Morales
 
circular linked list
Materi Kuliah Online
 
Conversion from infix to prefix using stack
Haqnawaz Ch
 
Ad

Similar to My lectures circular queue (20)

PPTX
Queue
Bhavesh Parmar
 
PPTX
Detalied information of queue
Smit Parikh
 
PPTX
08_Queues.pptx showing how que works given vertex
SadiaSharmin40
 
PPTX
apllicationsof queffffffffffffffffffffffffffffue.pptx
shesnasuneer
 
PDF
LEC4-DS ALGO.pdf
MuhammadUmerIhtisham
 
PPT
cp264_lecture18_queue.ppt
ssuserff72e4
 
PPTX
RPT_02_B_Queue presentation for FE students
AshishFamt
 
PPTX
queue_final.pptx
MeghaKulkarni27
 
PPT
10994103.ppt
SushmaG48
 
PPT
queue (1).ppt queue notes and ppt in Data Structures
nisharaheja1986
 
PPTX
Queues in C++
Vineeta Garg
 
PPT
Data structure.ppt
SajalFayyaz
 
PPT
intr_qyyuujjjjjjjkkkkkkkkkkkkkjkueue.ppt
MaximusAranha
 
PPTX
Lecture 7 data structures and algorithms
Aakash deep Singhal
 
PPTX
Unit 4 queue
Dabbal Singh Mahara
 
PPTX
4. Queues in Data Structure
Mandeep Singh
 
PPTX
Queue
Krishanu Ghosh
 
Detalied information of queue
Smit Parikh
 
08_Queues.pptx showing how que works given vertex
SadiaSharmin40
 
apllicationsof queffffffffffffffffffffffffffffue.pptx
shesnasuneer
 
LEC4-DS ALGO.pdf
MuhammadUmerIhtisham
 
cp264_lecture18_queue.ppt
ssuserff72e4
 
RPT_02_B_Queue presentation for FE students
AshishFamt
 
queue_final.pptx
MeghaKulkarni27
 
10994103.ppt
SushmaG48
 
queue (1).ppt queue notes and ppt in Data Structures
nisharaheja1986
 
Queues in C++
Vineeta Garg
 
Data structure.ppt
SajalFayyaz
 
intr_qyyuujjjjjjjkkkkkkkkkkkkkjkueue.ppt
MaximusAranha
 
Lecture 7 data structures and algorithms
Aakash deep Singhal
 
Unit 4 queue
Dabbal Singh Mahara
 
4. Queues in Data Structure
Mandeep Singh
 
Ad

My lectures circular queue

  • 1. Circular Queue & Priority Queue 1
  • 2. Queue (Linear Queue) • It is a linear data structure consisting of list of items. • In queue, data elements are added at one end, called the rear and removed from another end, called the front of the list. • Two basic operations are associated with queue: 7 1. “Insert” operation is used to insert an element into a queue. 2. “Delete” operation is used to delete an element from a queue. ● 6 FIFO list EEE 1 Queue: AAA, BBB, CCC, DDD, EEE AAA 2 BBB 3 4 CCC DDD 5 EEE 6 7 5 DDD 4 CCC 3 BBB • Example: 2 AAA 1 Front Rear 09/10/08 Rear Front 2
  • 3. 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 Figure: Circular Queue having Rear = 5 and Front = 0 a "Queue overflow" state. 3
  • 4. Algorithms for Insert and Delete Operations in Circular Queue For Insert Operation Insert-Circular-Q(CQueue, Rear, Front, N, Item) CQueue is a circular queue where to store data. Rear represents the location in which the data element is to be inserted and Front represents the location from which the data element is to be removed. Here N is the maximum size of CQueue and finally, Item is the new item to be added. Initailly Rear = 0 and Front = 0. 1. If Front = 0 and Rear = 0 then Set Front := 1 and go to step 4. 2. If Front =1 and Rear = N or Front = Rear + 1 then Print: “Circular Queue Overflow” and Return. 3. If Rear = N then Set Rear := 1 and go to step 5. 4. Set Rear := Rear + 1 5. Set CQueue [Rear] := Item. 6. Return 4
  • 5. For Delete Operation Delete-Circular-Q(CQueue, Front, Rear, Item) Here, CQueue is the place where data are stored. Rear represents the location in which the data element is to be inserted and Front represents the location from which the data element is to be removed. Front element is assigned to Item. Initially, Front = 1. 1. If Front = 0 then Print: “Circular Queue Underflow” and Return. /*..Delete without Insertion 2. Set Item := CQueue [Front] 3. If Front = N then Set Front = 1 and Return. 4. If Front = Rear then Set Front = 0 and Rear = 0 and Return. 5. Set Front := Front + 1 6. Return. 5
  • 6. Example: Consider the following circular queue with N = 5. 1. Initially, Rear = 0, Front = 0. 4. Insert 20, Rear = 3, Front = 0. Front Rear 2. Insert 10, Rear = 1, Front = 1. Rear Front 5. Insert 70, Rear = 4, Front = 1. Front Rear 3. Insert 50, Rear = 2, Front = 1. Front 6. Delete front, Rear = 4, Front = 2. Front Rear Rear 6
  • 7. 7. Insert 100, Rear = 5, Front = 2. Front 10. Delete front, Rear = 1, Front = 3. Rear Front Rear 8. Insert 40, Rear = 1, Front = 2. 11. Delete front, Rear = 1, Front = 4. Rear Rear Front Front 12. Delete front, Rear = 1, Front = 5. 9. Insert 140, Rear = 1, Front = 2. Rear As Front = Rear + 1, so Queue overflow. Rear Front Front 7
  • 8. Insertion qfull() { if(front==(rear+1)%size) return 1; else return 0; } int insert(int item) { if(qfull()) printf("circular q is full"); else { if(front==-1) front=rear=0; else rear=(rear+1)%size; que[rear]=item; } }
  • 9. Deletion int qempty() { if(front==-1) return 1; else return 0; } int delete() { int item; if(qempty()) printf("queue is empty"); else { item=que[front]; if(front==rear) { front=rear=-1; } else front=(front+1)%size; printf("the deleted item is%d ",item); }
  • 10. Display void display() { int i; if(qempty()) { printf("queue is empty"); return; } i=front; while(i!=rear) { printf(“%d”,que[i]); i=(i+1)%size; } Printf(“%d”,que[i]); }
  • 12. Priority Queue A Priority Queue – a different kind of queue. Similar to a regular queue: insert in rear, remove from front. Items in priority queue are ordered by some key Item with the lowest key / highest key is always at the front from where they are removed. Items then „inserted‟ in „proper‟ position Idea behind the Priority Queue is simple: Is a queue But the items are ordered by a key. Implies your ‘position’ in the queue may be changed by the arrival of a new item. 12
  • 13. Applications of Priority Queues Many, many applications. Scheduling queues for a processor, print queues, transmit queues, backlogs, etc.…. This means this item will be in the front of queue “Obtained” via a remove(). Note: a priority queue is no longer FIFO! You will still remove from front of queue, but insertions are governed by a priority. 13
  • 14. Priority Queues: Access remove() So, the first item has priority and can be retrieved (removed) quickly and returned to calling environment. Hence, „remove()‟ is easy (and will take O(1) time) insert() But, we want to insert quickly. Must go into proper position. For our purposes here: Implementing data structure: array; • slow to insert(), but for small number of items in the pqueue, and where insertion speed is not critical, • this is the simplest and best approach. 14
  • 15. Priority Queues Operations performed on priority queues 1) Find an element, 2) Insert a new element 3) Delete an element, etc. Two kinds of (Min, Max) priority queues exist: • In a Min priority queue, find/delete operation finds/deletes the element with minimum priority • In a Max priority queue, find/delete operation finds/deletes the element with maximum priority • Two or more elements can have the same priority 15
  • 16. Implementation of Priority Queues Implemented using heaps and leftist trees • Heap is a complete binary tree that is efficiently stored using the array-based representation • Leftist tree is a linked data structure suitable for the implementation of a priority queue 16
  • 17. Heap Operations When n is the number of elements (heap size), • Insertion  O(log2n) • Deletion  O(log2n) • Initialization  O(n) 17
  • 18. Insertion into a Max Heap 9 8 7 6 5 7 1 5 2 6 • New element is 5 • Are we finished? 18
  • 19. Insertion into a Max Heap 9 8 7 6 5 7 1 20 2 6 • New element is 20 • Are we finished? 19
  • 20. Insertion into a Max Heap 9 8 7 6 5 20 1 7 2 6 • Exchange the positions with 7 • Are we finished? 20
  • 21. Insertion into a Max Heap 9 20 7 6 5 8 1 7 2 6 • Exchange the positions with 8 • Are we finished? 21
  • 22. Insertion into a Max Heap 20 9 7 6 5 8 1 7 2 6 • Exchange the positions with 9 • Are we finished? 22
  • 23. Complexity of Insertion At each level, we do (1) work Thus the time complexity is O(height) = O(log2n), where n is the heap size 23
  • 24. Deletion from a Max Heap 20 15 7 6 5 9 1 7 2 8 6 • Max element is in the root • What happens when we delete an element? 24
  • 25. Deletion from a Max Heap 15 7 6 5 9 1 7 2 8 6 • After the max element is removed. • Are we finished? 25
  • 26. Deletion from a Max Heap 15 7 6 5 9 1 7 2 8 6 • Heap with 10 nodes. • Reinsert 8 into the heap. 26
  • 27. Deletion from a Max Heap 8 15 7 6 5 9 1 7 2 6 • Reinsert 8 into the heap. • Are we finished? 27
  • 28. Deletion from a Max Heap 15 8 7 6 5 9 1 7 2 6 • Exchange the position with 15 • Are we finished? 28
  • 29. Deletion from a Max Heap 15 9 7 6 5 8 1 7 2 6 • Exchange the position with 9 • Are we finished? 29
  • 30. Complexity of Deletion • The time complexity of deletion is the same as insertion • At each level, we do (1) work • Thus the time complexity is O(height) = O(log2n), where n is the heap size 30