lecture12: Largely based on slides by Cinda Heeren CS 225 UIUC 27th June, 2013
Announcements mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1) Source: http://i.dailymail.co.uk/i/pix/2010/08/04/article-1300471-0ab0a1d2000005dc-451 468x286.jpg
A Queue is a First-In, First-Out (FIFO) or Last-In, Last Out (LILO) structure. An analogy is a line of people waiting for an event. ADT functions include: 1 enqueue 2 dequeue 3 peek 4 size 5 empty
What use is a Queue? CPU and disk scheduling CS 241 (systems) Process communication (as a buffer) CS 241 (systems) Doing traversals (just like Stacks!) mp4, lab graphs, possibly mp7 Parsing CS 421 (compilers), CS 498jhm (natural language processing) Letting people into rides at an amusement park (how?)
What choices do we have? /** @file queue.h */ class Queue public: Queue(); // + big 3 void enqueue(const T & elem); T dequeue(); T peek() const; size_t size() const; bool empty() const; private: // what should go in here? };
Linked list or array? /** @file queue.h */ class Queue public: Queue(); // + big 3 void enqueue(const T & elem); T dequeue(); T peek() const; size_t size() const; bool empty() const; private: size_t _size; size_t _capacity; T* _data; }; /** @file queue.h */ class Queue public: Queue(); // + big 3 void enqueue(const T & elem); T dequeue(); T peek() const; size_t size() const; bool empty() const; private: // Node class... Node* head; size_t _size; };
Wait, we made these already... /** @file queue.h */ #include "vector.h" class Queue public: Queue(); // + big 3 void enqueue(const T & elem); T dequeue(); T peek() const; size_t size() const; bool empty() const; private: Vector<T> _array; }; /** @file queue.h */ #include "list.h" class Queue public: Queue(); // + big 3 void enqueue(const T & elem); T dequeue(); T peek() const; size_t size() const; bool empty() const; private: List<T> _list; };
Does any of this seem familiar yet? Is anyone having déjà vu? Stacks and are opposites: FIFO/LILO vs FILO/LIFO. They are each other s archnemesis.
The array implementation /** @file stack.cpp */ /** @file queue.h */ #include "vector.h" class Queue public: Queue(); // + big 3 void enqueue(const T & elem); T dequeue(); T peek() const; size_t size() const; bool empty() const; private: Vector<T> _array; }; Queue<T>::Queue(): _array(vector<t>()); void Queue<T>::enqueue(const T & elem) } T Queue<T>::dequeue() }
The linked list implementation /** @file stack.cpp */ /** @file queue.h */ #include "list.h" class Queue public: Queue(); // + big 3 void enqueue(const T & elem); T dequeue(); T peek() const; size_t size() const; bool empty() const; private: List<T> _list; }; Queue<T>::Queue(): _list(list<t>()); void Queue<T>::enqueue(const T & elem) } T Queue<T>::dequeue() }
Linked list intricacies (again!) When implementing a Queue with a linked list, does it matter where the front of the Queue is (head or tail)? Does it matter if the linked list is a SLL or a DLL? We are concerned with an efficient running time!
Your favorite For linked lists, assume the front of the queue is the head of the list and the back of the queue is the tail of the list. Function Array SLL DLL enqueue dequeue peek
Your favorite
Parting thoughts Again, consider the alternative implementation of a Queue with a (singly/doubly)-linked list: the front is the tail and the back is the head Can you think of any other implementations for a Queue besides linked lists and arrays? A circularly linked list with only a head pointer Two Stacks (mp4.1!) Can you make a Stack with two?