Queues 15-121 Fall 2020 Margaret Reid-Miller
Logistics Midsemester Grades: • 37% A • 30% B • 20% C • 13% < C (mostly poor homework grades) Exam 2: • Thursday Nov 5? • Tuesday Nov 9? Fall 2020 15-121 (Reid-Miller) 3
Today Today: • Stack applications • Queues • ListQueue class • Exercise: Reverse queue values • Circular ArrayQueue Fall 2020 15-121 (Reid-Miller) 4
Queues Fall 2020 15-121 (Reid-Miller) 5
Queue ADT “last in last out” Operations: • ENQUEUE – adds an element to the BACK of the queue • DEQUEUE – removes the element from the FRONT of the queue • PEEK – returns the element in the front of the queue without removing it (optional?) • Test if the queue is EMPTY Fall 2020 15-121 (Reid-Miller) 6
Queue Uses: • A store checkout line • A playlist of songs on a jukebox, mp3 player, etc. • Putting a new bottle of milk behind the old one. Fall 2020 15-121 (Reid-Miller) 7
Queue Operations public interface FIFOQueue<E> { void enqueue(E obj); // at back E dequeue(); // from front E peek(); // at front boolean isEmpty(); } Ideally, each of these operations should be O(1) time. Fall 2020 15-121 (Reid-Miller) 8
Queue Implementations Want O(1) for all operations, so what underlying data structure can we use? • ArrayList? No, one of the enqueue or dequeue must be O(n) • array? Yes, surprisingly. • linked list? Doubly ? Singly? No Yes Singly with reference to the tail? Yes Fall 2020 15-121 (Reid-Miller) 9
Linked List Queue Fall 2020 15-121 (Reid-Miller) 10
Queues using a singly-linked list null front back • Store the elements of the queue from front to back in order in the list. • Why do we need an additional reference to the tail? Fall 2020 15-121 (Reid-Miller) 11
Linked List Implementation Fields public class ListQueue<E> implements FIFOQueue<E> { private Node front; private Node back; references to nodes with front and back queue elements in list // methods (next slides) } Fall 2020 15-121 (Reid-Miller) 12
Linked List Implementation Constructor & isEmpty public ListQueue() { front = null; back = null; } indicates an empty queue public boolean isEmpty() { return (front == null); } Fall 2020 15-121 (Reid-Miller) 13
Linked List Implementation enqueue public void enqueue(E obj) { Node newNode = new Node(obj); if (back != null) { ❶ back.next = newNode; ❷ back = newNode; null } newNode else { back = newNode; _________________ ❶ front = newNode; _________________ } null } front back ❷ Fall 2020 15-121 (Reid-Miller) 14
Linked List Implementation dequeue public E dequeue() { if (front == null) throw new NoSuchElementException(); ❶ E element = front.data; ❷ front = front.next; ❶ element data ______________________________ if (front == null) back = null; return element; } null data ❷ front back Fall 2020 15-121 (Reid-Miller) 15
Linked List Implementation peek public E peek() { if (front == null) throw new NoSuchElementException(); return front.data; } Fall 2020 15-121 (Reid-Miller) 16
Exercise: Reverse // reverses the queue objects using a stack public static void reverse(FIFOQueue<String> q){ Fall 2020 15-121 (Reid-Miller) 17
Reverse // reverses the queue objects using a stack public static void reverse(FIFOQueue<String> q){ LIFOStack<string> s = new ArrayStack<String>(); while (!q.isEmpty()) s.push(q.dequeue()); while (!s.isEmpty()) q.enqueue(s.pop()); } Fall 2020 15-121 (Reid-Miller) 18
Reverse (recursively) // reverses the queue objects, recursively public static void reverse(Queue<String> q) { if (!q.isEmpty()) String front = q.dequeue(); reverse(q); q.enqueue(front); } Where’s the stack? Fall 2020 15-121 (Reid-Miller) 19
Array Queue Fall 2020 15-121 (Reid-Miller) 20
Queues using an array 0 1 2 3 4 5 6 7 8 9 10 11 FRONT BACK • Store the elements of the queue from front to back in order in the array. • What happens if we store the front of the queue always in position 0? No, dequeue is O(n) • Are all operations O(1)? Fall 2020 15-121 (Reid-Miller) 21
Queues using an array 0 1 2 3 4 5 6 7 8 9 10 11 FRONT BACK • Store the elements of the queue from front to back in order in the array. • What happens if we store the back of the queue always in last position in the array? No, enqueue is O(n) • Are all operations O(1)? Fall 2020 15-121 (Reid-Miller) 22
Queues using an array 0 1 2 3 4 5 6 7 8 9 10 11 FRONT BACK • Track positions of both front and back of queue. • To enqueue, increment back and add element at back. • To dequeue, remove the element at front and increment front . • What if the back is last element of the array and want to enqueue? Fall 2020 15-121 (Reid-Miller) 23
Queues using a “circular” array 0 1 2 3 4 5 6 7 8 9 10 11 BACK FRONT • Allow the queue to “wrap around” from the last cell of the array back to the first cell so no shifting is necessary . • Problem? • If the array is full, we can grow the array and copy the queue data into the new array starting at position 0 again . Fall 2020 15-121 (Reid-Miller) 24
Array Implementation Fields public class ArrayQueue<E> implements FIFOQueue<E> { private E[] dataArray; private int front; indices of front and back queue elements in array private int back; private int numElements; // methods (next slides) } Fall 2020 15-121 (Reid-Miller) 25
Array Implementation Constructor & isEmpty public ArrayQueue() { dataArray = (E[])new Object[1]; front = -1; back = -1; indicates an empty queue numElements = 0; } public boolean isEmpty() { return (numElements == 0); } Fall 2020 15-121 (Reid-Miller) 26
Array Implementation enqueue public void enqueue(E element) { numElements == dataArray.length if (______________________________________) grow(); (back+1) % dataArray.length back = ____________________________; dataArray[back] = element; if (front == -1) front = back; numElements++; } Fall 2020 15-121 (Reid-Miller) 27
Array Implementation grow 0 1 2 3 4 5 What's wrong? If you keep dequeuing, it tries to BACK FRONT dequeue at index 6 . 0 1 2 3 4 5 6 7 8 9 10 11 BACK FRONT Fall 2020 15-121 (Reid-Miller) 28
Array Implementation grow 0 1 2 3 4 5 BACK FRONT 0 1 2 3 4 5 6 7 8 9 10 11 FRONT BACK Fall 2020 15-121 (Reid-Miller) 29
Array Implementation grow private void grow() { E[] newArray = (E[])new Object[numElements*2]; int from = front; for (int to = 0; to < numElements; to++) { newArray[to] = dataArray[from]; (from + 1) % numElements from = ___________________________; } front = 0; back = numElements-1; dataArray = newArray; } Fall 2020 15-121 (Reid-Miller) 30
Array Implementation peek public E peek() { numElements == 0 if (__________________________) throw new NoSuchElementException(); return dataArray[front]; } Fall 2020 15-121 (Reid-Miller) 31
Array Implementation dequeue public E dequeue() { E obj = peek(); dataArray[front] = null; if (front == back) { // was one element ______________________________________ front = -1; back = -1; } else ______________________________________ front = (front+1)%dataArray.length; numElements--; return obj; } Fall 2020 15-121 (Reid-Miller) 32
Other uses for queues • Printer queues • Packet router • Simulating a queuing system • Supermarket checkout lanes • Highway traffic congestion models • Internet traffic Fall 2020 15-121 (Reid-Miller) 33
Java Queue<E> interface • The java.util package includes a Queue<E> interface. • Queue<E> includes specifications for these methods: • boolean offer(E item) enqueue • E element() * peek • E peek() ** • E remove() * dequeue • E poll() ** * Throws an exception if queue is empty. ** Returns null if the queue is empty. Fall 2020 15-121 (Reid-Miller) 34
Using Java Queue<E> • LinkedList<E> implements Queue<E> . • Example: A queue of customers Queue<Customer> customerQ = new LinkedList<Customer>(); Customer c = new Customer("Andrew"); customerQ.offer(c); // OK customerQ.addFirst(c); // BAD (Why?) Fall 2020 15-121 (Reid-Miller) 35
Recommend
More recommend