Download presentation
Presentation is loading. Please wait.
Published byKelly Manning Modified over 10 years ago
1
Lecture Queues
2
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
3
Applications of Queues Input and output buffers Managing batch jobs Management of a printer queue OS Scheduling
4
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
5
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
6
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
7
24. // Use the dequeue method 25. ListNode removedObj = null; 26. try 27. { 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 }
8
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
9
To enqueue (insert) an item we need: q[back ++] = item; To dequeue (delete) an item we would need: item = q[ front ++]; Array Queue Implementation
10
After inserting A, B, C A B C [0] [1] [2] [3] [4] front = 0 back = 3 q Array Queue Implementation
11
After deleting A, B C [0] [1] [2] [3] [4] front = 2 back = 3 q Array Queue Implementation
12
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
13
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 DE [0] [1] [2] [3] [4] back = 0 front = 2 q
14
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! FCDEFEDCG [0] [1] [2] [3] [4] back = 1 front = 2 back = front = 2
15
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 FCDE [0] [1] [2] [3] [4] back = 1 front = 2
16
Any other solution? Keep a count of the number of items in the queue
17
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.