CS 231 Data Structures and Algorithms Fall 2018 DDL & Queue Lecture 20 October 22, 2018 Prof. Zadia Codabux 1
Agenda Mid Semester Analysis Doubly Linked List Queue 2
Administrative None 3
Doubly Linked List Operations completed: addfirst add(index) removelast addlast removefirst get(index) 4
Recall - Singly Linked List Complexity AddFirst/ RemoveFirst? Add/Remove (at index)? Get (using index)? AddLast/RemoveLast? 5
Recall - Singly Linked List Complexity AddFirst/ RemoveFirst : O(1) Add/Remove (at index) : O(n) Get (using index) : O(n) AddLast/RemoveLast : O(n) 6
Doubly Linked List Complexity AddFirst/ RemoveFirst? Add/Remove (at index)? Get (using index)? AddLast/RemoveLast? 7
Doubly Linked List Complexity AddFirst/ RemoveFirst : O(1) Add/Remove (at index) : O(n) Get (using index) : O(n) AddLast/RemoveLast : O(1) 8
Queue 9
Queue A queue is a FIFO sequence. Addition takes place only at the tail, and removal takes place only at the head. Like a stack, queue can contain any object (Generics). The basic operations for queues are identical to those for stacks, except the standard name for adding elements into a queue is enqueue (instead of push) and the name for removing elements from a queue is dequeue. 10
Applications Line of cars at a light Line of people at cafe Printer buffer 11
Queue Operations The basic operations are : enqueue(x): add an item at the tail dequeue(): remove the item at the head peek(): return the item at the head (without removing it) size(): return the number of items in the queue isempty(): return whether the queue has no items 12
Queue Operations Queue.add(e): It throws an exception if the operation fails. Same for Queue.remove() and Queue.element(): Queue.offer(e): It returns a special value(e.g. null or false) if the operation fails. Same for Queue.poll() and Queue.peek(): OPERATION THROWS EXCEPTION SPECIAL VALUE Insert add(e) offer(e) Remove remove() poll() Examine element() peek() Source: https://docs.oracle.com/javase/8/docs/api/java/util/queue.html 13
Implementation We can use many ways to implement queues: Array: enqueue is easy, dequeue requires shifting the rest in the array ArrayList: enqueue is easy, ArrayList takes care of the shifting cased by dequeue, resize memory is necessary SinglyLinkedList: dequeue is easy, but enqueue need to traverse to the end of the list DoublyLinkedList: enqueue (addlast) and dequeue (removefirst/remove(0)) are easy. 14
Array-Based Queue We can use an array of fixed capacity to store items as a queue. enqueue: append new items to the end of the queue dequeue: remove items from the front of the queue, and shift the rest of the items. Enqueue is okay, but dequeue is not efficient due to the shifting. How to make the array-based queue more efficient? Have two indexes (like the head and tail pointers): rear index and front index. Enqueue new items to the rear index Dequeue items from the front index 15
Array-Based Queue Issue: Rear index may reach the last array index (reach the capacity) while the front index may have available space (due to the dequeue) Solution: Allocate more space (double the size) Wrap around (circular array). 16
Queue Operations 17
Queue Operations 18
Queue Operations 19
Circular Array In the circular array, we use modular arithmetic to calculate the from and rear indexes. Assume the length of the array is m. front++ gets replaced with front = (front + 1) % m; first element in the queue rear++ gets replaced with rear = (rear + 1) % m; next available space 20
Circular Array But it is still possible for the buffer to fill if using circular array. How can we tell? If the rear wrap around and catches up with the front (if (rear + 1) % m == front), it is full. Or we can keep track of the count and the capacity. If the array is full, we can also reallocate the array and copy the queue data into the new array starting at position 0 again. 21
Array-based Queue Implementation Let s write some code! 22
Queue Complexity Enqueue()? Dequeue()? Peek()? 23