Stacks and Queues - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Stacks and Queues

Description:

Don't represent real-world data but more programmer tools temporary for running of program ... Programmer's tool e.g., search a graph ... – PowerPoint PPT presentation

Number of Views:209
Avg rating:3.0/5.0
Slides: 15
Provided by: UST
Category:

less

Transcript and Presenter's Notes

Title: Stacks and Queues


1
Stacks and Queues
  • Joe Komar

2
Stacks and Queues Overview
  • Dont represent real-world data but more
    programmer tools temporary for running of
    program
  • Only one item can be read or removed at a given
    time (versus arrays)
  • More abstract than arrays the underlying
    implementation is hidden and its interface is
    what is important

3
Stacks
  • Only the last item inserted in a stack is
    available
  • Used to parse arithmetic expressions
  • Used with other data structures
  • Binary trees
  • Graphs
  • Concepts also used in hardware design
  • First-in, Last-out
  • Pushing placing an item on the top of the stack
  • Popping removing an item from the top of the
    stack
  • (see example applet) (see example code)

4
Stack Examples
  • Reversing a string
  • Push each character
  • Pop each character
  • (see example code)
  • Delimiter matching
  • Left delimiter must have matching right delimiter
  • Later opening delimiters need to be closed before
    previous opening delimiters
  • (see example code)

5
Efficiency of Stacks and Queues
  • Time is not dependent on how many items there are
    in the stack or queue
  • Therefore, O(1) time for both insertion and
    deletion (there is NO searching)

6
Queues
  • British for line
  • First-in, first-out
  • Programmers tool e.g., search a graph
  • Real-world modeling waiting lines of people,
    planes, data packets, etc.
  • Insert (put, add, enque) an item at the rear
    (back, tail, end) of the queue
  • Remove (delete, get, deque) an item from the
    front (head) of the queue
  • (see example applet and circular queue ring
    buffer) (see example code)

7
Priority Queues
  • Just like a queue (first-in, first-out)
  • Ordered by some key value, usually from low to
    high
  • Used as a programmers tool
  • Used for other data structures such as Weighted
    Graphs
  • Used in operating systems (preemtive,
    multitasking)
  • Often use a heap as its underlying implementation
    (quick insertion)
  • (see example applet) (see example code)

8
Priority Queue Efficiency
  • Deletion O(1)
  • Insertion O(N)

9
Parsing Arithmetic Expressions
  • Uses stacks
  • Transform the expression (infix format) into
    postfix format (complex)
  • Evaluate the postfix expression (easy)
  • ((AB)C)-D ? ABCD-
  • AB(C-D/(EF)) ? ABCDEF/-

10
Evaluating an expression
  • 5
  • 6
  • 2
  • End
  • 562

11
Evaluating an Expression
  • (
  • 5
  • 6
  • )
  • 2
  • End
  • (56)2

12
Translating Infix to Postfix
  • AB-C ABC-
  • ABC ABC
  • A(BC) ABC
  • AB(C-D) ABCD-
  • (see translation rules in text)
  • (see example code)

13
Evaluating Postfix Expressions
  • Creating postfix from infix is not trivial at
    all!
  • Rules for postfix evaluation
  • If operand, push it into the stack
  • If operator, pop the two operands from the stack
    and apply the operator to them, then push the
    result
  • (see example code)

14
Summary
  • Stacks, queues, and priority queues are typically
    programmer tools
  • Only one data item is accessed
  • Stack is first-in, last-out
  • Queues are first-in, first-out
  • Priority queues retain ordered data
  • Can all be implemented with arrays or other
    mechanisms such as linked lists
  • Stacks are indispensable for arithmetic
    expression evaluation
Write a Comment
User Comments (0)
About PowerShow.com