Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt...

17
Lecture Queues

Transcript of Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt...

Page 1: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

Lecture

Queues

Page 2: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

Queues

• The name ‘Queue’ derives from objects that have to queue in order to be dealt with

• A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO) abstract data type

Page 3: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

Applications of Queues

• Input and output buffers

• Managing batch jobs

• Management of a printer queue

• OS Scheduling

Page 4: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

Operation On a Queue: Informal Abstract Definition

• Create: i.e. initialise an empty queue

• Enqueue: add data item to the back of the queue

• Dequeue: remove data item from the front of the queue – if queue is empty throw an exception

• IsEmpty: determine if queue is empty

Page 5: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

class QueueInheritance extends StudentList{

public QueueInheritance () { super(“Queue");}public void enqueue (String nameIn, int ageIn) {

insertAtBack(nameIn, ageIn); } public ListNode dequeue() throws EmptyListException {

return removeFromFront(); }public boolean isEmpty () { return super.isEmpty(); }public void printQueue() { printList(); }

} //end of class QueueInheritance

Linked List Queue: Inheritance Example

Page 6: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

QueueInheritance theQueue = new QueueInheritance();

// Use the enqueue method

theQueue.enqueue("Alice", 20 ); theQueue.printQueue();

theQueue.enqueue("John", 22); theQueue.printQueue();

theQueue.enqueue("Rita", 21); theQueue.printQueue();

theQueue.enqueue("Bob", 23); theQueue. printQueue();

Linked List Queue: Inheritance Example

Page 7: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

24. // Use the dequeue method25. ListNode removedObj = null;26. try27. {28. while ( true )29. {30. removedObj = theQueue.dequeue();31. System.out.println( removedObj.name.toString() + "

dequeued" );32. theQueue.printQueue();33. }34. }35. catch ( EmptyListException e )36. {37. System.err.println( "\n" + e.toString() );38 }

Page 8: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

• front and back are the indices

• Avoid overflow – more than what the array can hold

• front = back = 0 is the ‘empty condition’ & prevents underflow – remove item from empty queue

[0] [1] [2] [3] [4]

front = back = 0

q

An empty queue

Array Queue Implementation

Page 9: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

• To enqueue (insert) an item we need:

q[back ++] = item;

• To dequeue (delete) an item we would need:

item = q[ front ++];

Array Queue Implementation

Page 10: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

• After inserting A, B, C

A B C

[0] [1] [2] [3] [4]

front = 0 back = 3

q

Array Queue Implementation

Page 11: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

• After deleting A, B

C

[0] [1] [2] [3] [4]

front = 2 back = 3

q

Array Queue Implementation

Page 12: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

• After inserting D

• Any problems if we try to insert E? Overflow!

• Any remedies? Shift the entire queue after every

dequeue operation, but very costly!

C D

[0] [1] [2] [3] [4]

front = 2 back = 4

q

Page 13: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

• Is there a better solution? Circular Queue

• Allow array element to be ‘wrapped around’

• So, now we can insert E

• But enqueue and dequeue operation must also be re-defined (see example)

C D E

[0] [1] [2] [3] [4]

back = 0 front = 2

q

Page 14: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

• Now consider inserting F, G

• Array is full – any attempt to insert another item causes overflow

• Do you the problem? How do you test for empty queue? i.e. underflow

• back = = front is no longer the condition for testing underflow!

F C D E F EDCG

[0] [1] [2] [3] [4] [0] [1] [2] [3] [4]

back = 1 front = 2 back = front = 2

Page 15: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

• Any solutions?

• Allow queue to grow only as large as one less than it maximum size ….

• Queue becomes full when back points to the location immediately before front

F C D E

[0] [1] [2] [3] [4]

back = 1 front = 2

Page 16: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

• Any other solution?

Keep a count of the number of items in the queue

Page 17: Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt with A queue is a First-In-First-Out(FIFO) or a Last-In-Last-Out(LILO)

Re-defining enqueue and dequeue operation

• Enqueue

q[back] = item;

back = ( back + 1) % n;

• Dequeue

item = q[front];

front = (front + 1) % n;

where n is the size of the array and % is the remainder operator.