1 / 15

Lecture 10

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:

Télécharger la présentation

Lecture 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 10 • Stacks • Queues

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

  3. 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)

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

  5. 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”)

  6. 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)

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

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

  9. 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.

  10. 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.

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

  12. 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)

  13. 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

  14. 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)

  15. Circular arrays • head and count • head and tail • head and tail and queueEmpty

More Related