Lecture Queues. The name ‘Queue’ derives from objects that have to queue in order to be dealt...
-
Upload
kelly-manning -
Category
Documents
-
view
214 -
download
0
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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/1.jpg)
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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/2.jpg)
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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/3.jpg)
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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/4.jpg)
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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/5.jpg)
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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/6.jpg)
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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/7.jpg)
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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/8.jpg)
• 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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/9.jpg)
• 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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/10.jpg)
• 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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/11.jpg)
• 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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/12.jpg)
• 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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/13.jpg)
• 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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/14.jpg)
• 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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/15.jpg)
• 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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/16.jpg)
• 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)](https://reader036.fdocuments.us/reader036/viewer/2022082701/551ae901550346f70d8b4a05/html5/thumbnails/17.jpg)
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.