+ All Categories
Home > Documents > Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same...

Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same...

Date post: 26-Mar-2015
Category:
Upload: brianna-gonzalez
View: 215 times
Download: 3 times
Share this document with a friend
Popular Tags:
29
Transcript
Page 1: Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Page 2: Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Page 3: Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.

Queues

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

were received

Page 4: Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs 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)

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

Queue operations

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

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

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

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

Queues using arrays (first try)

Head

Tail

Empty queue (Tail == Head)

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

Queues using arrays (first try)

Head

Tail

Add item ‘T’ at location Tail

T

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

Queues using arrays (first try)

Head

Tail

Add item ‘H’ at location Tail

T H

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

Queues using arrays (first try)

Head

Tail

Add item ‘I’ at location Tail

T H I

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

Queues using arrays (first try)

Head

Tail

Remove item ‘T’ from Head

H I

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

Queues using arrays (first try)

Head

Tail

Continue until Tail == ArraySizeQueue full?

A Q U E U E

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

Queues using arrays (first try)

Head

Tail

Must shift queue contents back tostart of array - inefficient!

A Q U E U E

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

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

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

A circular array

01

2

MaxSize - 1

MaxSize - 2

MaxSize - 3

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

A queue using a circular array

MaxSize - 1

Tail

HeadEmpty queueTail == Head

0

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

A queue using a circular array

T

MaxSize - 1

Tail

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

0

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

A queue using a circular array

TH

MaxSize - 1

Tail

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

0

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

A queue using a circular array

TH

MaxSize - 1

Tail

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

0

I

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

A queue using a circular array

H

MaxSize - 1

Tail

Head

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

0

I

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

A queue using a circular array

MaxSize - 1

Tail

Head

Continue untilTail == MaxSize - 1

0

E

U

E

U

QA

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

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

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

Empty and Full Queue Tests

Empty queue condition: Head = = Tail

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

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

Queue ADT in Java

Constructor isempty isfullJoinLeave

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

Constructor

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

Head = Tail = 0 ;}

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

Head = Tail = 0 ;}

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

Test for empty queue

public boolean isempty()

{

return Head == Tail) }

}

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

Test for full queue

public boolean isfull(){

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

return true; } else { return false;

}}

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

Adding an element

public void Join(int val)

{ Queue[Tail] = val ;

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

}

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

Remove an element

public int Leave()

{ int Removed ;

Removed = Queue[Head];

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

return Removed ;

}


Recommended