Lecture 10 b Stacks b Queues Stacks b
Lecture 10 b Stacks b Queues
Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In, First-Out b Analogy: a stack of plates push pop 2
Stack Data Structure b A stack is a data structure supporting the following operations: • • • b is. Empty() : check if empty. top(): examine the top element. push(item): add a new top element. pop(): remove the top element. clear(): clear the stack. All operations should be constant-time (do not depend on size of stack)
Applications of Stacks b b Managing call frames of subprograms Expression evaluation Elimination of recursion “Searching” for a solution
Stack implementations b Array (or vector) • space efficient • per operation time is constant, but significant delays at times of reallocation b Linked list • space overhead may be significant • space for stack is allocated incrementally (not in “bursts”)
Array Based Implementation 7 gets overwritten tos = 2 push(5) push(3) 5 0 3 8 1 2 3 4 push(7) pop() push(8)
List Based Implementation tos = 7 3 5 push(5) push(3) push(7) pop()
List Based Implementation tos 8 3 7 This object is now garbage. 5 push(5) push(3) push(7) pop() push(8)
Analysis b b It is easy to check that all operations in the linked list case are O(1). Since arrays are fixed length, our implementation dynamically doubles the size of allocated array when needed. This happens if we perform a push operation when the allocated array is full. In this case, a fresh array of double the size is allocated and the contents of the old array are copied into this one.
Analysis (continued) b b Thus most push are O(1) except the ones where doubling and copying happens. (the other operations are O(1) clearly. ) Since these are infrequent, we can spread the cost over a sequence of stack operations. With this, a sequence of M stack operations is O(M). This technique is called amortization.
Queues b A queue is similar to a list but adds items only to the end of the list and removes them from the front b It is called a FIFO data structure: First-In, First-Out b Analogy: a line of people at a bank teller’s window enqueue dequeue 11
Queue Data Structure b A queue is a data structure supporting the following operations: • • b is. Empty() : check if empty. enqueue(item): add a new element at the rear of the queue. dequeue(): remove the front element. clear(): clear the queue. All operations should be constant-time (do not depend on size of queue)
Applications of Queues b Managing system resources (eg, printer queues) b “Searching” for a solution b Simulations (eg: airplanes landing and taking off at an airport) b Graph traversals
Queue implementations b Vector • unacceptably slow dequeue (moves up all items) b Array • space efficient • using fixed-size circular array also very time efficient b Linked list • space overhead may be significant • allows maximum flexibility (queue can grow and shrink as needed)
Circular arrays b head and count b head and tail and queue. Empty
- Slides: 15