Kelsey Sam Andreea Liam End of Year Kristen Celebration Lunch!! Bei Gabby Wednesday, April 24, 12:15 Tolshoi Samah Kendade 307 Cade Angela Marc Jessie Claudia 1 Stack vs. Queue Stack is Last In First Out (LIFO) 3 2 http:/ /www.ifimages.com/photos/DL7xPn8R8CHcLkIzGfqnFFJck/ 1 author-562/Stack-white-plates-background.jpg Queue is First In First Out (FIFO) 1 2 3 http:/ /www.mathworks.com/matlabcentral/fx_files/24238/2/ queue_line_2.jpg 2 Queue Examples Deli counter Checkout line Printer queue Ticket window Processes ready for CPU Calls on hold Data to be sent over network Cards in a draw/ discard pile 3 Monday, April 22, 13
Queue Data structure that removes items in the same order they are added public class CallQueue { public void enqueue (Call item); / / Add a caller to end public Call front(); / / Get caller waiting the longest public Call dequeue(); / / Remove caller waiting the longest public boolean isEmpty(); / / Are any callers waiting? public int size(); / / How many callers are waiting? } 4 Queue Generic Data structure that removes items in the same order they are added public interface Queue<T> { public void enqueue (T item); / / Add an item to end public T front(); / / Get item waiting the longest public T dequeue(); / / Remove item waiting the longest public boolean isEmpty(); / / Are any items waiting? public int size(); / / How many items are waiting? } 5 Managing Queue Data We need to know: What is at the front of the queue Maintain elements in the order added Where the next thing added to the queue should go How should we organize the data? Array? ArrayList? Linked list? 6 Monday, April 22, 13
Queue Implemented with a Linked List head = null tail = null 7 Queue Implemented with a Linked List tail head 1 enqueue (1); 8 Queue Implemented with a Linked List tail head 1 2 enqueue (1); enqueue (2); 9 Monday, April 22, 13
Queue Implemented with a Linked List tail head 1 2 3 enqueue (1); enqueue (2); enqueue (3); 10 Queue Implemented with a Linked List tail 2 3 head enqueue (1); enqueue (2); enqueue (3); dequeue (); 11 Finding a Path in a Maze (Non-recursive) Place the starting square in a queue while (the queue is not empty) { remove the front of the queue; for each neighbor of the removed square { if it has not been explored { if it is the destination { return true; } add it to the queue } } } 12 Monday, April 22, 13
Queue Implemented with an Array 1 2 3 enqueue (1); enqueue (2); enqueue (3); 13 Queue Implemented with an Array 1 2 3 enqueue (1); enqueue (2); enqueue (3); dequeue (); 14 Queue Implemented with an Array 2 3 enqueue (1); enqueue (2); enqueue (3); dequeue (); dequeue is O(n) 15 Monday, April 22, 13
Queue Implemented with an Array (Idea 2) 1 2 3 4 5 6 7 8 front enqueue (1); enqueue (2); enqueue (3); enqueue (4); enqueue (5); enqueue (6); enqueue (7); enqueue (8); 16 dequeue (); Queue Implemented with an Array (Idea 2) 2 3 4 5 6 7 8 front enqueue (1); dequeue (); enqueue (2); enqueue (3); enqueue (4); enqueue (5); enqueue (6); enqueue (7); enqueue (8); 17 dequeue (); Queue Implemented with an Array (Idea 2) 3 4 5 6 7 8 front enqueue (1); dequeue (); enqueue (2); dequeue (); enqueue (3); enqueue (4); enqueue (5); enqueue (6); enqueue (7); enqueue (8); dequeue (); 18 Monday, April 22, 13
Queue Implemented with an Array (Idea 2) 4 5 6 7 8 front enqueue (1); dequeue (); enqueue (2); dequeue (); enqueue (3); enqueue (4); Problem: We can only enqueue (5); ever put n elements in enqueue (6); the queue! enqueue (7); enqueue (8); dequeue (); 19 Queue Implemented with a CircularArray 1 2 3 first last numItems = 3 enqueue (1); enqueue (2); enqueue (3); dequeue (); 20 Queue Implemented with a CircularArray 2 3 first last numItems = 2 enqueue (1); enqueue (2); enqueue (3); dequeue (); enqueue (4); 21 Monday, April 22, 13
Queue Implemented with a CircularArray 2 3 4 first last numItems = 2 enqueue (1); enqueue (5); enqueue (2); enqueue (6); enqueue (3); enqueue (7); dequeue (); dequeue (); enqueue (4); enqueue (8); 22 Queue Implemented with a CircularArray 3 4 5 6 7 8 first last numItems = 6 enqueue (1); enqueue (5); enqueue (2); enqueue (6); enqueue (3); enqueue (7); dequeue (); dequeue (); enqueue (4); enqueue (8); 23 Queue Implemented with a CircularArray 3 4 5 6 7 8 first last numItems = 6 enqueue (1); enqueue (5); enqueue (9); enqueue (2); enqueue (6); enqueue (3); enqueue (7); dequeue (); dequeue (); enqueue (4); enqueue (8); 24 Monday, April 22, 13
Queue Implemented with a CircularArray 9 3 4 5 6 7 8 first last numItems = 7 enqueue (1); enqueue (5); enqueue (9); enqueue (2); enqueue (6); enqueue (3); enqueue (7); dequeue (); dequeue (); enqueue (4); enqueue (8); 25 Data Members for Circular Array int first; / / index of front of queue int last; / / index of last element of queue int numItems; / / how many in queue int capacity; / / how many queue can hold T[] values; / / array for data 26 Queue in <<interface>> Iterable Collection Hierarchy <<interface>> Collection <<interface>> <<interface>> <<abstract>> Queue List AbstractCollection <<abstract>> AbstractList LinkedList Stack ArrayList 27 Monday, April 22, 13
Recommend
More recommend