Download presentation
Presentation is loading. Please wait.
Published byPierce Walker Modified over 8 years ago
1
Week 3 - Friday
2
What did we talk about last time? Stacks Array implementation of a stack
5
Bitmap Manipulator
6
Impromptu Student Lecture
8
A queue is a simple data structure that has three basic operations (very similar to a stack) EnqueuePut an item at the back of the queue DequeueRemove an item from the front of the queue FrontReturn the item at the front of the queue A queue is considered FIFO (First In First Out) or LILO (Last In Last Out)
9
Queues are useful whenever you want to keep track of the order of arrival A line in a fast food restaurant A job in a printer queue A buffer for managing data
10
A queue is a little bit harder to implement than a stack with an array The trouble is that you're enqueuing and dequeuing from different ends Removing something from the front seems to imply that you'll need to shift over all the contents of the array Enter the circular array!
11
A circular array is just a regular array However, we keep a start index as well as a size that lets us start the array at an arbitrary point Then, the contents of the array can go past the end of the array and wrap around The modulus operator ( % ) is a great way to implement the wrap around
12
1. Starting array 2. Enqueue 9 3. Dequeue 4. Dequeue 5. Enqueue 14 6. Dequeue 183219 Start Size = 4 7183219 StartSize = 5 718321 StartSize = 4 143219 Start Size = 4 14219 Start Size = 4 3219 StartSize = 3
13
Advantages: Dequeue is Θ(1) Front is Θ(1) Disadvantages Enqueue is Θ(n) in the very worst case, but not in the amortized case
14
public class ArrayQueue { private int[] data = new int[10]; private int start = 0; private int size = 0; public void enqueue(int value) {} public int dequeue() {} public int front() {} public int size() {} } public class ArrayQueue { private int[] data = new int[10]; private int start = 0; private int size = 0; public void enqueue(int value) {} public int dequeue() {} public int front() {} public int size() {} }
20
Java does have a Stack class which extends Vector The Deque (double ended queue) interface is preferred A double ended queue can be used as either stack or queue Stack OperationDeque Method Push addFirst(T element) Pop removeFirst() Top peekFirst() Size size() Queue OperationDeque Method Enqueue addLast(T element) Dequeue removeFirst() Front peekFirst() Size size()
21
Since Deque is an interface, we have to have classes that can implement it ArrayDeque is an implementation of a double ended queue that uses a circular array for backing Probably the best choice for both queues and stacks in terms of speed and memory use addFirst() (push) is O(1) amortized addLast() (enqueue) is O(1) amortized removeFirst() (pop and dequeue) is O(1) peekFirst() (top and front) is O(1)
22
Good old LinkedList is an implementation of a double ended queue that uses a doubly linked list for backing Generally slower than ArrayDeque, but the important operations are O(1) without being amortized addFirst() (push) is O(1) addLast() (enqueue) is O(1) removeFirst() (pop and dequeue) is O(1) peekFirst() (top and front) is O(1)
23
A priority queue is like a regular queue except that items are not always added at the end They are added to the place they need to be in order to keep the queue sorted in priority order Not all requests are created equal A higher priority job can come along and jump in front of a lower priority job Unfortunately, we have to wait for the heap data structure to implement priority queues efficiently
25
Linked lists
26
Keep reading section 1.3 Finish Assignment 2 Due tonight by 11:59pm Keep working on Project 1 Due next Friday, September 18 by 11:59pm
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.