50% found this document useful (2 votes)
810 views29 pages

DataStructure - Circular, Double Ended, Priority Queues

This document discusses different types of queues including circular queues, double ended queues (deques), and priority queues. It provides examples of how each type of queue is represented and the algorithms for common operations like insert, delete, isEmpty, and isFull. For circular queues, it shows how the head and tail pointers change during enqueue and dequeue operations to allow the queue to wrap around. For deques, it describes two variations and how front and rear pointers are adjusted during insertions and deletions at either end. Priority queues are represented using separate queues for each priority level.

Uploaded by

aliyaraza1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
810 views29 pages

DataStructure - Circular, Double Ended, Priority Queues

This document discusses different types of queues including circular queues, double ended queues (deques), and priority queues. It provides examples of how each type of queue is represented and the algorithms for common operations like insert, delete, isEmpty, and isFull. For circular queues, it shows how the head and tail pointers change during enqueue and dequeue operations to allow the queue to wrap around. For deques, it describes two variations and how front and rear pointers are adjusted during insertions and deletions at either end. Priority queues are represented using separate queues for each priority level.

Uploaded by

aliyaraza1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Data Structures & Algorithms

Circular Queues Double Ended Queues Priority Queues

Today Topics
Problem with Simple/scalar Queues? Solution
Circular Queues What are the circular Queue

Operations on Circular Queues


Enqueue Dequeue IsEmptyQueue IsFullQueue

Recap
What is simple/scalar queue Operation of simple queue

Problem with simple Queue

How head and tail Change


head increases by 1 after each dequeue( ) tail increases by 1 after each enqueue( )
tail Now: 49 After enqueue: 48 47 4 3 tail 2 1 0 head head

49
After dequeue: 49

48

47

3 2 tail

1
head 1

48

47

CS 103

Solution: A Circular Queue


Allow the head (and the tail) to be moving targets When the tail end fills up and front part of the array has empty slots, new insertions should go into the front end
tail 49 48 47 head 4 3 2 1 0

Next insertion goes into slot 0, and tail tracks it. The insertion after that goes into a lot 1, etc.
CS 103 6

Illustration of Circular Queues


Current state:
49 tail 48 47 4 head 3 2 1 0

After One Call to enqueue()


head
49 48 47 4 3 2 1

tail
0

After One Call to enqueue()


head 3 49 48 47 4
CS 103

tail 2 1 0
7

Circular Queue
When elements are deleted from the front, their spaces cannot be used to store new elements. To solve this problem, circular queue is used
Q[8] Q[7] Q[6] Q[5] Q[4]

Q[1]
Q[2]

Q[3]

Algorithms of CQInsert Operation


CQInsert (X) Algorithm for Insert element into the Circular Queue 1. Start 2. if front = 1 and rear = Max then Print Queue Overflow! else if rear + 1 = front then Print Queue Overflow! else if front = 0 and rear=0 then front = rear = 1 else if rear = Max then rear = 1 else rear = rear + 1 end if Q[rear] = X end if End

Algorithms of CQDelete Operation


CQDelete () Algorithm for delete element into the Circular Queue 1. Start 2. if front = 0 then Print Queue Underflow! else E = Q[front] if front = rear then front = rear = 0 else if front = Max then front = 1 else front = front + 1 end if end if End

DeQue
Word deque is a short form of double-ended queue. Deque defines a data structure in which item can be added or deleted at either the front or rear end. But no changes can be made elsewhere in the list. Deque is a generalization of both a stack and a queue.

DeQue
There are two variations of deques. These are: Input Restricted Deque It allows insertions only at one end but allows deletions at both ends. Output Restricted Deque It allows deletions only at one end but allows insertions at both end

Representation of Deque
delete

insert

insert

delete

a[1] 34

a[2] 12

a[3] 53

a[4] 61

a[5] 9

a[6] 23

a[7] -8

a[8] 15

a[9] 24

a[10] 42

front

Deque

rear

Implementation of Deque
When an item is inserted at the Front of DEQ, then front is decreased by 1.
When an item is inserted at the Rear of DEQ, then rear is increased by 1. When an item is deleted at the Front of DEQ, then front is increased by 1. When an item is deleted at the Rear of DEQ, then rear is decreased by 1.

Implementation of Deque
In the following figure, the DEQ has some items stored in it
Front

Y
Deque

Rear

front = 2

rear = 4

The first element at the front of the deque is empty and two elements at the rear are also empty.

Implementation of Deque
If another item XX is added at the rear, then the DEQ and values of front and rear will be :

Front

X
front = 2

Y
Deque

Z
rear = 5

XX

Rear

Implementation of Deque
If two items are deleted at the front and one item is deleted at the rear, then the DEQ and values of front and rear will be:

Front Deque

Z
front = 4 rear = 4

Rear

Algorithms of DeQInsert Operation


DeQInsert (X, Side) Algorithm for Insert element into the Deque 1. Start 2. if front = 0 and rear = 0 then front = rear = 1 DQ[front] = X Return end if 3. if Side = 1 then if front > 1 then front = front 1 DQ[front] = X else Print No space at front of the Deque!

Algorithms of DeQInsert Operation


else
end if if rear < Max then rear = rear + 1 DQ[rear] = X else Print No space at rear of the Deque! end if

end if 4. End

Algorithms of DeQDelete Operation


DeQDelete (Side) Algorithm for delete element into the Deque 1. Start 2. If front = 0 and rear = 0 then Print DeQueue Underflow! Return end if 3. if front = rear then E= DQ[front] front = rear = 0 Return end if

Algorithms of DeQDelete Operation


4. if Side = 1 then E = DQ[front] front = front + 1 else E = DQ[rear] rear = rear - 1 end if 5. End

Priority Queues
Priority queue is a collection of elements where the elements are stored according to their priority levels. The order in which the elements should get added or removed is decided by the priority of the element. Following rules are applied to maintain a priority queue: The element with a higher priority is processed before any element of lower priority. If there are elements with the same priority, then the element added first in the queue would get processed.

Example for Priority Queues


Priority queues are used for implementing job scheduling by the operating system where jobs with higher priorities are to be processed first.
Another application of priority queues is simulation systems where priority corresponds to event times.

Representation of Priority Queues


Priority queues can be represented in the several ways.
Best way to represent it is to use a separate queue for each level of priority. Each such queue is represented in circular fashion and has its own front and rear.

Representation of Priority Queues


Usually, an array of arrays, i.e. a two-dimensional array is used to represent.

2
3 4

A
M

B
N

C
O P

Algorithms of Insert Operation


PQInsert (X, P) Algorithm for Insert element into the Priority Queue 1. Start 2. if rear[P] >= Max then Print Queue Overflow! else rear[P] = rear[P] + 1 Q[P][rear[P]] = X if front[P] = 0 then front[P] = 1 end if end if 3. End

Algorithms of Delete Operation


PQDelete (P) Algorithm for delete element into the Priority Queue 1. Start 2. if front[P] = 0 then Print Queue Underflow! else E = Q[P][front[P]] if front[P] = rear[P] then front[P] = rear[P] = 0 else front[P] = front[P] + 1 end if end if 3. End

Assignment
Given a stack S and a queue Q, write a computer program in any Object Oriented Programming language that have two procedures FILLQ_WITHS which will empty the contents of the Stack S and insert them into the queue Q and FILLS_WITHQ which will fill the stack S with the elements deleted from the queue Q. Implement the procedures with S and Q having an Array representation.

Next Lecture
Pointer Review Linked List Representation of Link List Operations of Linked List Circular Linked List Double Linked List (Two-Way List) Representation of Double Linked List Operations of Double Linked List

You might also like