150 likes | 159 Views
Learn about stacks and queues, their operations, implementations, applications, and analysis. Master the concepts of LIFO (Last-In, First-Out) and FIFO (First-In, First-Out) structures.
E N D
Lecture 10 • Stacks • Queues
push pop Stacks • A stack ADT is linear • Items are added and removed from only one end of a stack • It is therefore LIFO: Last-In, First-Out • Analogy: a stack of plates
Stack Data Structure • A stack is a data structure supporting the following operations: • isEmpty(): 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 • Managing call frames of subprograms • Expression evaluation • Elimination of recursion • “Searching” for a solution
Stack implementations • Array (or vector) • space efficient • per operation time is constant, but significant delays at times of reallocation • Linked list • space overhead may be significant • space for stack is allocated incrementally (not in “bursts”)
0 1 2 3 4 Array Based Implementation push(5) tos = 2 7 gets overwritten push(3) 5 3 8 push(7) pop() push(8)
tos 5 3 7 List Based Implementation push(5) = push(3) push(7) pop()
tos 5 3 7 8 List Based Implementation push(5) push(3) push(7) This object is now garbage. pop() push(8)
Analysis • 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) • 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.
dequeue enqueue Queues • A queue is similar to a list but adds items only to the end of the list and removes them from the front • It is called a FIFO data structure: First-In, First-Out • Analogy: a line of people at a bank teller’s window
Queue Data Structure • A queue is a data structure supporting the following operations: • isEmpty(): 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 • Managing system resources (eg, printer queues) • “Searching” for a solution • Simulations (eg: airplanes landing and taking off at an airport) • Graph traversals
Queue implementations • Vector • unacceptably slow dequeue (moves up all items) • Array • space efficient • using fixed-size circular array also very time efficient • Linked list • space overhead may be significant • allows maximum flexibility (queue can grow and shrink as needed)
Circular arrays • head and count • head and tail • head and tail and queueEmpty