29
Data Structures & Algorithms Circular Queues Double Ended Queues Priority Queues

DataStructure - Circular, Double Ended, Priority Queues

Embed Size (px)

Citation preview

Data Structures & Algorithms

Circular QueuesDouble Ended QueuesPriority Queues

Problem with Simple/scalar Queues? Solution

Circular Queues What are the circular Queue

Operations on Circular Queues Enqueue Dequeue IsEmptyQueue IsFullQueue

Today Topics

Recap What is simple/scalar queue Operation of simple queue

Problem with simple Queue

CS 103 5

How head and tail Change

head increases by 1 after each dequeue( ) tail increases by 1 after each enqueue( )

012474849 4 3

012474849 4 3

012474849 4 3

Now:

After enqueue:

After dequeue:

tail head

tail head

tailhead

CS 103 6

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

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

012474849

headtail

34

CS 103 7

Illustration of Circular Queues Current state:

After One Call to enqueue()

After One Call to enqueue()

012474849 34tail

head

012474849 34

tailhead

012474849 34

head tail

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[1]

Q[2]

Q[3]

Q[4]Q[5]

Q[6]

Q[7]

Q[8]

Algorithms of CQInsert OperationCQInsert (X)

Algorithm for Insert element into the Circular Queue1. Start2. 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 thenfront = rear = 1

else if rear = Max thenrear = 1

elserear = rear + 1

end ifQ[rear] = X

end if End

Algorithms of CQDelete OperationCQDelete ()

Algorithm for delete element into the Circular Queue1. Start2. if front = 0 then

Print “Queue Underflow!”else

E = Q[front]if front = rear then

front = rear = 0else if front = Max then

front = 1else

front = front + 1end 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 DequeIt allows insertions only at one end but allows deletions at both ends.

Output – Restricted DequeIt allows deletions only at one end but allows insertions at both end

Representation of Deque

a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]

34 12 53 61 9 23 -8 15 24 42

front rear

insert insertdelete delete

Deque

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

X Y ZFront Rear

front = 2 rear = 4

Deque

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 :

X Y Z XXFront Rear

front = 2 rear = 5

Deque

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:

ZFront Rear

front = 4 rear = 4

Deque

Algorithms of DeQInsert OperationDeQInsert (X, Side)

Algorithm for Insert element into the Deque1. Start2. if front = 0 and rear = 0 then

front = rear = 1DQ[front] = XReturn

end if3. if Side = 1 then

if front > 1 thenfront = front – 1DQ[front] = X

elsePrint “No space at front of the

Deque!”

Algorithms of DeQInsert Operationend if

elseif rear < Max then

rear = rear + 1DQ[rear] = X

elsePrint “No space at rear of the

Deque!”end if

end if4. End

Algorithms of DeQDelete OperationDeQDelete (Side)

Algorithm for delete element into the Deque1. Start

2. If front = 0 and rear = 0 thenPrint “DeQueue Underflow!”Return

end if

3. if front = rear thenE= DQ[front]front = rear = 0Return

end if

Algorithms of DeQDelete Operation

4. if Side = 1 thenE = DQ[front]

front = front + 1else

E = DQ[rear]rear = rear - 1

end if5. 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.

1 X Y

2 A B C

3 M N O P

4

Algorithms of Insert OperationPQInsert (X, P)

Algorithm for Insert element into the Priority Queue1. Start2. if rear[P] >= Max then

Print “Queue Overflow!”else

rear[P] = rear[P] + 1Q[P][rear[P]] = X

if front[P] = 0 then front[P] = 1

end ifend if

3. End

Algorithms of Delete OperationPQDelete (P)

Algorithm for delete element into the Priority Queue1. Start2. if front[P] = 0 then

Print “Queue Underflow!”else

E = Q[P][front[P]]if front[P] = rear[P] then

front[P] = rear[P] = 0else

front[P] = front[P] + 1end if

end if3. 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