Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.

Post on 26-Mar-2015

215 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

Transcript

Queues

Printer queuesSeveral jobs submitted to printerJobs form a queueJobs processed in same order as they

were received

Properties of queues

First In First Out (FIFO)Data added at one end only (the Tail of

the queue)Data removed at other end only (the

Head of the queue)

Queue operations

Initialise queueAdd item (to tail of queue)Remove item (from head of queue)Check if queue emptyCheck if queue full

Queues using arrays

Use array to store queue elementsDefine a data item Head which

identifies the item at Head of queueDefine a data item Tail which

identifies the first empty location after last item in queue

Tail identifies location where next item is placed in queue

Queues using arrays (first try)

Head

Tail

Empty queue (Tail == Head)

Queues using arrays (first try)

Head

Tail

Add item ‘T’ at location Tail

T

Queues using arrays (first try)

Head

Tail

Add item ‘H’ at location Tail

T H

Queues using arrays (first try)

Head

Tail

Add item ‘I’ at location Tail

T H I

Queues using arrays (first try)

Head

Tail

Remove item ‘T’ from Head

H I

Queues using arrays (first try)

Head

Tail

Continue until Tail == ArraySizeQueue full?

A Q U E U E

Queues using arrays (first try)

Head

Tail

Must shift queue contents back tostart of array - inefficient!

A Q U E U E

Circular Queue

Use a circular queueConsider (perceive?) the array as a

circular structure (i.e. as if the last element of the array is connected/joined to the first element of the array)

The benefit of this insight is that we never have to shift data

A circular array

01

2

MaxSize - 1

MaxSize - 2

MaxSize - 3

A queue using a circular array

MaxSize - 1

Tail

HeadEmpty queueTail == Head

0

A queue using a circular array

T

MaxSize - 1

Tail

HeadAdd ‘T’ at TailTail = (Tail + 1) % MaxSize

0

A queue using a circular array

TH

MaxSize - 1

Tail

HeadAdd ‘H’ at TailTail = (Tail +1) % MaxSize

0

A queue using a circular array

TH

MaxSize - 1

Tail

HeadAdd ‘I’ at TailTail = (Tail +1) % MaxSize

0

I

A queue using a circular array

H

MaxSize - 1

Tail

Head

Remove ‘T’ from HeadHead = (Head +1) % MaxSize

0

I

A queue using a circular array

MaxSize - 1

Tail

Head

Continue untilTail == MaxSize - 1

0

E

U

E

U

QA

A queue using a circular array

MaxSize - 1

Tail

Head

0

E

U

E

U

QA

Add ‘Z’ at TailTail = (Tail +1) % MaxSizei.e. [(MaxSize - 1) + 1] % MaxSize = MaxSize % MaxSize = 0

Z

Empty and Full Queue Tests

Empty queue condition: Head = = Tail

Full queue condition: (Tail + 1) % MaxSize = = Head

Queue ADT in Java

Constructor isempty isfullJoinLeave

Constructor

public QueueOfInts(){ Queue = new int[10] ;

Head = Tail = 0 ;}

public QueueOfInts(int capacity){ Queue = new int[capacity] ;

Head = Tail = 0 ;}

Test for empty queue

public boolean isempty()

{

return Head == Tail) }

}

Test for full queue

public boolean isfull(){

if ((Tail + 1) % Queue.length == Head) {

return true; } else { return false;

}}

Adding an element

public void Join(int val)

{ Queue[Tail] = val ;

Tail = (Tail + 1) % Queue.length;

}

Remove an element

public int Leave()

{ int Removed ;

Removed = Queue[Head];

Head = (Head + 1) % Queue.length ;

return Removed ;

}

top related