Download presentation
Presentation is loading. Please wait.
Published byTobias Holland Modified over 9 years ago
1
Chapter 14 Queues
2
First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome method Various queue implementations – Linked list based – Array based Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 2
3
Queues Standard queue operations: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 3
4
Pre Lab 3 -Q#1 Assume that ticketLine is a newly created Queue. What is the state of the Queue after the following series of statements have been executed? ticketLine.enqueue("Smith"); ticketLine.enqueue("Hu"); String name = ticketLine.dequeue(); ticketLine.enqueue("Mudahar"); ticketLine.enqueue("Bailey"); ticketLine.enqueue("Field"); ticketLine.enqueue("Davis"); ticketLine.enqueue("Matthison"); ticketLine.dequeue(); ticketLine.enqueue("Able"); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 4
5
Queues in the Java Collections API Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 5 Throws exceptionReturns special value Insertadd(e)offer(e)offer(e) (true/false) Removeremove()poll()poll() (null) Examineelement()peek() The Java Collections API is not consistent about its implementation of collections – http://docs.oracle.com/javase/8/docs/api/index.html http://docs.oracle.com/javase/8/docs/api/index.html For queues, the API provides a Queue interface, then various classes such as LinkedList implement the interface
6
Implementing a Queue with Links Since operations work on both ends of the queue, we'll use both front and rear references Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 6
7
package jsjf; import jsjf.exceptions.*; /** * LinkedQueue represents a linked implementation of a queue. * * @author Java Foundations * @version 4.0 */ public class LinkedQueue implements QueueADT { private int count; private LinearNode head, tail; /** * Creates an empty queue. */ public LinkedQueue() { count = 0; head = tail = null; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 7
8
/** * Adds the specified element to the tail of this queue. * @param element the element to be added to the tail of the queue */ public void enqueue(T element) { LinearNode node = new LinearNode (element); if (isEmpty()) head = node; else tail.setNext(node); tail = node; count++; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 8
9
/** * Removes the element at the head of this queue and returns a reference to it. * @return the element at the head of this queue * @throws EmptyCollectionException if the empty */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("queue"); T result = head.getElement(); head = head.getNext(); count--; if (isEmpty()) tail = null; return result; } // Other methods public T first() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("queue"); return head.getElement(); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 9 // methods to be implmented public boolean isEmpty() {…} public int size() { …} public String toString() {…}
10
Implementing a Queue with an Array If we implement a queue as we did a stack, one end would be fixed at index 0: The problem is that (unlike a stack) a queue operates at both ends To be efficient, we must avoid shifting elements Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 10
11
Implementing a Queue with an Array A better solution is to treat the array as circular A circular array is a a regular array that is treated as if it loops back around on itself That is, the last index is thought to precede index 0 We use two integers to keep track of where the front and rear of the queue are at any given time Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 11
12
Implementing a Queue with an Array A queue implemented using a circular queue: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 12
13
Implementing a Queue with an Array At some point, the elements of the queue may straddle the end of the array: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 13
14
After A-H have been enqueued: After A-D have been dequeueed: After I, J, K, and L have been enqueued: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 14
15
Implementing a Queue with an Array Both the front and rear index values are incremented, wrapping back to 0 when necessary Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 15
16
package jsjf; import jsjf.exceptions.*; /** * CircularArrayQueue represents an array implementation of a queue in * which the indexes for the front and rear of the queue circle back to 0 * when they reach the end of the array. * * @author Java Foundations * @version 4.0 */ public class CircularArrayQueue implements QueueADT { private final static int DEFAULT_CAPACITY = 100; private int front, rear, count; private T[] queue; Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 16
17
/** * Creates an empty queue using the specified capacity. * @param initialCapacity the initial size of the circular array queue */ public CircularArrayQueue (int initialCapacity) { front = rear = count = 0; queue = (T[]) (new Object[initialCapacity]); } /** * Creates an empty queue using the default capacity. */ public CircularArrayQueue() { this(DEFAULT_CAPACITY); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 17
18
/** * Adds the specified element to the rear of this queue, expanding * the capacity of the queue array if necessary. * @param element the element to add to the rear of the queue */ public void enqueue(T element) { if (size() == queue.length) expandCapacity(); queue[rear] = element; rear = (rear+1) % queue.length; count++; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 18
19
/** * Creates a new array to store the contents of this queue with * twice the capacity of the old one. */ private void expandCapacity() { T[] larger = (T[]) (new Object[queue.length *2]); for (int scan = 0; scan < count; scan++) { larger[scan] = queue[front]; front = (front + 1) % queue.length; } front = 0; rear = count; queue = larger; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 19
20
/** * Removes the element at the front of this queue and returns a * reference to it. * @return the element removed from the front of the queue * @throws EmptyCollectionException if the queue is empty */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("queue"); T result = queue[front]; queue[front] = null; front = (front+1) % queue.length; count--; return result; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 20
21
Pre-Lab 3 #2 Explain how a queue can be implemented using an array, where the enqueue and the dequeue operations are both constant time operations (for simplicity, assume that the capacity of the array is not expanded when the queue becomes full.) Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 21
22
Pre-Lab 3 #3 Suppose there was no count variable stored for the linked list based implementation of a queue. Explain why it would not be possible to implement a constant time size() operation? Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 22
23
Pre-Lab 3 #4 Suppose there was no count variable stored for the array based implementation of a queue. Explain why it would still be possible to implement a constant time size() operation? Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 23
24
Pre-Lab 3 #5 & 6 5. Given a linked list based implementation of a Queue, in what case will the following code for the enqueue method fail? Assume that rear is a reference to the end of the queue. public void enqueue(T element) { LinearNode newNode = new LinearNode(element); rear.setNext(newNode); rear = newNode; count++; } 6. How can the enqueue method from the previous problem be corrected? Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 24
25
Pre-Lab 3 #7 Correct the bug in the enqueue method shown below which is for a queue implemented as a circular array. public void enqueue(T element) { if(count == queue.length) expandCapacity(); queue[rear] = element; rear = rear + 1; count++; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 25
26
Comparing queue implementations Array Based & Linked List Based – Space comparison – Time comparison Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 26
27
Dequeue http://docs.oracle.com/javase/8/docs/api/java/u til/Deque.html http://docs.oracle.com/javase/8/docs/api/java/u til/Deque.html Linked List based implementation – Doubly-linked lists Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 27
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.