Docsity

Queues - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Queues - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Queues,Stacks and

Topological sort

Docsity.com

STACK QUEUES

Docsity.com

Stacks

  • All access is restricted to the most recently inserted elements
  • Basic operations are push, pop, top.

2013/4/23 4

Stack

push (^) pop, top

Docsity.com

Non-Computer Examples

  • Stack of papers
  • Stack of bills
  • Stack of plates
  • Expect O ( 1 ) time per stack operation. (In other words, constant time per operation, no matter how many items are actually in the stack).

Balanced Symbol Algorithm

  • Make an empty stack.
  • Repeatedly read tokens; if the token is
    • an opening symbol, push it onto the stack
    • a closing symbol
      • and the stack is empty, then report an error;
      • otherwise pop the stack and verify that the popped symbol is a match (if not report an error)
  • At the end of the file, if the stack is not empty, report an error.

Example

  • Input: {()}
  • Push {
  • Push (; stack has {(
  • Pop; popped item is ( which matches ). Stack now has {.
  • Pop; popped item is { which matches }.
  • End of file; stack is empty, so all is good.

Procedure Stack

  • Matching symbols is similar to procedure call and procedure return, because when a procedure returns, it returns to the most recently active procedure. Procedure stack can be used to keep track of this.
  • Abstract idea: when procedure call is made, save current state on a stack. On return, restore the state by popping the stack.

Activation Records

  • Actual implementation is slightly different (note that text describes the abstract implementation).
  • The top of stack can store the current procedure environment.
  • When procedure is called, the new enviromment is pushed onto stack.
  • On return, old enviroment is restored by pop

Queues

  • All access is restricted to the least recently inserted elements
  • Basic operations are enqueue, dequeue, getFront.

2013/4/23 13

enqueue^ Queue dequeue getFront

Docsity.com

Examples

  • Line for Panther tickets
  • Line printer queue
  • Queue is a British word for line.
  • Expect O ( 1 ) time per queue operation because it is similar to stack. The word "queueing" and its derivatives are the only English words with five consecutive vowels.

Array Implementations

  • Stack can be implemented with an array and an integer top that stores the array index of the top of the stack.
  • Empty stack has top equal to -1.
  • To push, increment the top counter, and

write in the array position.

  • To pop, decrement the top counter.

Stages

2013/4/23 17

top(-1)

A top(0) (^) A

B top(1)

Docsity.com

Running Time

  • Without array doubling, all operations are constant time, and do not depend on number of items in the stack.
  • With array, doubling, a push could occasionally be O ( N ). However, it is essentially O ( 1 ) because each array doubling that costs N assignments is preceded by N / non-doubling pushes.

Queues: Simple Idea

  • Store items in an array with front item at index zero and back item at index Back.
  • Enqueue is easy: increment Back.
  • Dequeue is inefficient: all elements have to be shifted.
  • Result: Dequeue will be O ( N ).