CISC220 Spring 2010 James Atlas Lecture 10: Queues
Objectives for Today Stacks Queues Deques Array based implementation Reading - K+W Chap 5, 6
Stacks
Stack ADT peek() - returns the top element of the stack without removing it pop() - removes (and returns) the top element of the stack push(x) - pushes x onto the top of the stack LIFO structure - last in, first out
Stack Uses Expression evaluation and syntax parsing –Reverse Polish notation * 3 + Program Execution Stack
Queue
Queue ADT front() - returns the front element of the queue without removing it dequeue() - removes (and returns) the front element of the queue queue(x) - queues x at the end of the queue FIFO structure - first in, first out
Queue using a LinkedList Multiple Inheritence: class LinkedList : public Collection, public Stack, public Queue front() = first() dequeue() = remove(0) queue(x) = addLast(x) Time complexity?
Queue using an Array (Vector) Linked lists require extra storage of pointers for next front() = get(0) dequeue() = remove(0) queue(x) = insert(x, length) Time complexity? How can we reduce this?
Circular Array
Implementing Queue With Circular Array
Implementing Queue With Circular Array (2)
Implementing Queue With Circular Array (3)
Reallocating a Circular Array
Deque
Deque ADT back() front() pushBack(x) pushFront(x) popBack() popFront()
Doubly-linked list
Inserting into a Double-Linked List DNode* sharon = new DNode("Sharon"); // Link new DNode to its neighbors sharon->next = sam; // Step 1 sharon->prev = sam->prev; // Step 2
Inserting into a Double-Linked List (2) // Link old predicessor of sam to new predicessor. sam->prev->next = sharon; // Step 3 // Link to new predicessor. sam->prev = sharon; // Step 4
Removal from a Double-Linked List harry->prev->next = harry->next; // Step 1 harry->next->prev = harry->prev; // Step 2 delete harry;