YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: Lecture 4 Queue

Data Structures & Algorithms

Dated: 06-12-2010

Page 2: Lecture 4 Queue

What are Queues?

Representation of Queues

Operations on Queues QInsert QDelete

Today Topics

Page 3: Lecture 4 Queue

What are Queues?Queue can be defined as:

A queue is an ordered collection of items from which items may be deleted at one end (called the front of the queue) and into which items may be inserted at the other end (called the rear of the queue).

A queue is a waiting line – seen in daily life A line of people waiting for a bank teller A line of cars at a toll both

Page 4: Lecture 4 Queue

queue Features:

A list  structure with two access points called the front and rear.

All insertions (enqueue) occur at the rear and deletions (dequeue) occur at the front.

Varying length (dynamic). Homogeneous components Has a First-In, First-Out characteristic (FIFO)

Page 5: Lecture 4 Queue

application

Page 6: Lecture 4 Queue

Application of QueuePurpose of queue is to provide some form of buffering. Queues are used for:

Process Management: In timesharing system, programs are added to a queue and executed one after the other.

Buffer between the fast computer and a slow printer.

In queues, scheme used is FIFO or LILO

Page 7: Lecture 4 Queue

implementation of Queues There are two ways of implementing a queue:

Array (Static) Link List (Dynamic)

A queue can be implemented with an array and two integers.

The first integer front used for deletion item from queue and rear used for insert item in queue

Page 8: Lecture 4 Queue

Array Implementation

Page 9: Lecture 4 Queue

How to implement queue Decide how many data member are needed to

implement the queue. We need at least four types of variable.

An array to store the element Variables queuefront to keep track of the first element Queuerear to keep track of the last elelment. Maxsize variable

How to use rear and front to access the queue element.????

How they indicate that queue is full or empty.

Page 10: Lecture 4 Queue

Bounds of Queue

If FRONT: = NULL “empty” If REARE := NUL “empty” If REARE := N “overflow, full” If FRONT=REARE != NULL then the

queue contain only one element. If FRONT = REARE???????? Very

interesting situation.

Page 11: Lecture 4 Queue

Representation of Queues

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

Page 12: Lecture 4 Queue

Queues Working

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

Currently Queue Status

front = 0 rear = 0

Page 13: Lecture 4 Queue

Queues Working

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

32

Currently Queue Status

front = 1 rear = 1

QInsert(32)frontrear

Page 14: Lecture 4 Queue

Queues Working

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

32 44

Currently Queue Status

front = 1 rear = 2

QInsert(44)front rear

Page 15: Lecture 4 Queue

Queues Working

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

32 44 65

Currently Queue Status

front = 1 rear = 3

QInsert(65)

front rear

Page 16: Lecture 4 Queue

Queues Working

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

32 44 65 25

Currently Queue Status

front = 1 rear = 4

QInsert(25)

front rear

Page 17: Lecture 4 Queue

Queues Working

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

32 44 65 25 53

Currently Queue Status

front = 1 rear = 5

QInsert(53)

front rear

Page 18: Lecture 4 Queue

Queues Working

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

44 65 25 53

Currently Queue Status

front = 2 rear = 5

QDelete()

front rear

Page 19: Lecture 4 Queue

Queues Working

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

65 25 53

Currently Queue Status

front = 3 rear = 5

QDelete()

front rear

Page 20: Lecture 4 Queue

Think about it

What happen when Rear= 10 Front = 9

????????? And you want to insert. Two solution::::::

Check the front, if there is room then slides all the element to fist position, but well when queue is small.

Use circular array.

Page 21: Lecture 4 Queue

In case of circular queue how to set rear point Rear= (rear + 1) % maxsize; how??????? If Rear < maxsize -1, then

Rear + 1 <= mzxsize -1 and so (Rear +1) % maxsize= Rear + 1.

If Rear == maxsize -1 (that is rear point to the last position) then Rear + 1 == maxsize and so (Rear + 1) % maxsize==0. in this case Rear is set to 0.

And same can be done for Front adjustment.

Page 22: Lecture 4 Queue

Operations on Queue Generally, Queue is implemented with only

two principle operations

InitializeQueue queue to an empty state destroyQuere revome all the elements isEmpthyQueue isFullQueue Front rear Insertion: adds an item to a queue Deletion: delete an item from the queue

Page 23: Lecture 4 Queue

Algorithms of Insert Operation QINSERT(QUEUE, N, FRONT, REAR, ITEM)

This algorithm insert an element ITEM into a queue.

Setp1. [Queue already filled]

if FRONT := 1 and REAR:= N,

write “overflow” and return

Step 2. [Find new value of REAR]

if FRONT := NULL [Queue initially empty]

Set FRONT:= 1 and REAR:= 1

Else if REAR:=N than

Set REAR:=1

Else

Set REAR:=REAR+1

[End of if structure]

Step 3. Set QUEUE[REAR]:=ITEM [this insert new item ]

Step 4. exit

Page 24: Lecture 4 Queue

Algorithms of Insert OperationQInsert (X)

Algorithm for Insert element into the Queue1. Start2. if rear >= Max then

Print “Queue Overflow!”else

rear = rear + 1Q[rear] = X

if front = 0 thenfront = 1

end ifend if

End

Page 25: Lecture 4 Queue

Algorithms of DELETE Operation QDELETE(QUEUE, N, FRONT, REAR, ITEM)

This algorithm delete an element ITEM into a queue.

Setp1. [Queue already empty]

if FRONT := NULL write “UNDERFLOW”

return

Step 2. set ITEM:= QUEUE[FORNT]

Step 3. [Find new value of FRONT]

if FRONT := REAR than [Queue has only one element]

Set FRONT:= NULL and REAR:= NULL

Else if FRONT:=N than

Set FRONT;=1

Else

Set FRONT:=FRONT+1

[End of if structure]

Step 5. exit

Page 26: Lecture 4 Queue

Algorithms of Delete OperationQDelete ()

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

Print “Queue Underflow!”else

E = Q[front]if front = rear then

front = rear = 0else

front = front + 1end if

end if3. End

Page 27: Lecture 4 Queue

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]

Page 28: Lecture 4 Queue

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

Page 29: Lecture 4 Queue

Algorithms of CQDelete OperationCQDelete ()

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

Print “Queue Underflow!”else

E = Q[rear]if front = rear then

front = rear = 0else if front = Max then

front = 1else

front = front + 1end if

end if End

Page 30: Lecture 4 Queue

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.

Page 31: Lecture 4 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

Page 32: Lecture 4 Queue

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

Page 33: Lecture 4 Queue

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.

Page 34: Lecture 4 Queue

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.

Page 35: Lecture 4 Queue

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

Page 36: Lecture 4 Queue

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

Page 37: Lecture 4 Queue

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 if[specify front or rear side to insert value]

3. [insert value at front of the queue] if Side = 1 thenif front > 1 thenfront = front – 1DQ[front] = XelsePrint “No space at front of the Deque!”

Page 38: Lecture 4 Queue

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

Page 39: Lecture 4 Queue

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

Page 40: Lecture 4 Queue

Algorithms of DeQDelete Operation

4. if Side = 1 thenif front = Max then

E = DQ[front]front = 0

elseE = DQ[front]

front = front + 1end if

elseE = DQ[rear]

rear = rear - 1end if

5. End

Page 41: Lecture 4 Queue

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.

Page 42: Lecture 4 Queue

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.

Page 43: Lecture 4 Queue

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.

Page 44: Lecture 4 Queue

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

Page 45: Lecture 4 Queue

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


Related Documents