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

Some concept of Data Structures are Abstract, Balance Factor, Complete Binary Tree, Dynamically, Storage, Implementation, Sequential Search, Advanced Data Structures, Graph Coloring Two, Insertion Sort. Main points of this lecture are: Queues, Stacks, Topological Sort, Queues, Definitions, Stack, First in Last Out, Queue, First in First Out, Inserted Elements

Typology: Slides

2012/2013

Uploaded on 04/30/2013

patel
patel 🇮🇳

3.8

(15)

80 documents

1 / 44

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Queues,Stacks and
Topological sort
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

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