Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 10 b Stacks b Queues. 2 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,

Similar presentations


Presentation on theme: "Lecture 10 b Stacks b Queues. 2 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,"— Presentation transcript:

1 Lecture 10 b Stacks b Queues

2 2 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 poppush

3 Stack Data Structure b A stack is a data structure supporting the following operations: isEmpty() : check if empty.isEmpty() : check if empty. top(): examine the top element.top(): examine the top element. push(item): add a new top element.push(item): add a new top element. pop(): remove the top element.pop(): remove the top element. clear(): clear the stack.clear(): clear the stack. b All operations should be constant-time (do not depend on size of stack)

4 Applications of Stacks b Managing call frames of subprograms b Expression evaluation b Elimination of recursion b “Searching” for a solution

5 Stack implementations Stack implementations b Array (or vector) space efficientspace efficient per operation time is constant, but significant delays at times of reallocationper operation time is constant, but significant delays at times of reallocation b Linked list space overhead may be significantspace overhead may be significant space for stack is allocated incrementally (not in “bursts”)space for stack is allocated incrementally (not in “bursts”)

6 Array Based Implementation 0 1 2 3 4 tos = push(5) push(3) push(7) pop() push(8) 2 538 7 gets overwritten

7 List Based Implementation push(5) push(3) push(7) pop() 5=37 tos

8 List Based Implementation This object is now garbage. push(5) push(3) push(7) pop() push(8) 53 7 tos 8

9 Analysis b It is easy to check that all operations in the linked list case are O(1). b Since arrays are fixed length, our implementation dynamically doubles the size of allocated array when needed. b This happens if we perform a push operation when the allocated array is full. b In this case, a fresh array of double the size is allocated and the contents of the old array are copied into this one.

10 Analysis (continued) b Thus most push are O(1) except the ones where doubling and copying happens. (the other operations are O(1) clearly.) b Since these are infrequent, we can spread the cost over a sequence of stack operations. b With this, a sequence of M stack operations is O(M). b This technique is called amortization.

11 11 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

12 Queue Data Structure b A queue is a data structure supporting the following operations: isEmpty() : check if empty.isEmpty() : check if empty. enqueue(item): add a new element at the rear of the queue.enqueue(item): add a new element at the rear of the queue. dequeue(): remove the front element.dequeue(): remove the front element. clear(): clear the queue.clear(): clear the queue. b All operations should be constant-time (do not depend on size of queue)

13 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

14 Queue implementations Queue implementations b Vector unacceptably slow dequeue (moves up all items)unacceptably slow dequeue (moves up all items) b Array space efficientspace efficient using fixed-size circular array also very time efficientusing fixed-size circular array also very time efficient b Linked list space overhead may be significantspace overhead may be significant allows maximum flexibility (queue can grow and shrink as needed)allows maximum flexibility (queue can grow and shrink as needed)

15 Circular arrays Circular arrays  head and count  head and tail  head and tail and queueEmpty


Download ppt "Lecture 10 b Stacks b Queues. 2 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,"

Similar presentations


Ads by Google