Stacks - PowerPoint PPT Presentation

About This Presentation
Title:

Stacks

Description:

Stacks & Queues. CSC 172. SPRING 2002. LECTURE 4. Agenda. Stack. Definition. Implementation ... If the stack is empty, report an error ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 31
Provided by: thaddeusf
Category:
Tags: stack | stacks

less

Transcript and Presenter's Notes

Title: Stacks


1
Stacks Queues
  • CSC 172
  • SPRING 2002
  • LECTURE 4

2
Agenda
  • Stack
  • Definition
  • Implementation
  • Analysis
  • Queue
  • Definition
  • Implementation
  • Analysis

3
Workshop sign-up
  • Still time
  • Dave Feil-Seifer df001i_at_mail.rochester.edu
  • Ross Carmara rc001i_at_mail.rochester.edu
  • Read Culotta article on team work in science

4
HOMEWORK 2
  • AU
  • 6.6.1,6.6.2(Java),6.6.4,6.6.6
  • 6.7.1
  • 6.8.1,6.8.2,6.8.3

5
Stacks
  • LIFO (last in, first out) data structure
  • Restricts access to the most recently inserted
    item
  • Stacks are an ADT with operations push pop
  • void push(Object o)
  • void pop()
  • Object top()

6
STACK
pop, top
push
7
STACK INTERFACE
  • public interface Stack
  • void push(Object x) //insert
  • void pop() // remove
  • Object top() // find
  • Object topAndPop() // find remove
  • boolean isEmpty()
  • void makeEmpty()

8
Example Balance checking
  • Compilers check for syntax errors
  • One syntax error is mismatched delimiters
  • must match
  • ( must match )
  • must match
  • () is legal, but () is not
  • Could you write a balance checker?

9
A useful stack algorithm
  • Balance checking
  • Make an empty stack
  • Read symbols until the end of file
  • If the token is an opening symbol, push it onto
    the stack
  • If the token is a closing symbol
  • If the stack is empty, report an error
  • Otherwise, pop the stack, if the symbol is not
    the corresponding opening symbol report an error
  • At EOF if stack is not empty, report an error

10
Trace by hand
  • (())
  • ()

11
A useful stack algorithm
  • Postfix evaluation
  • We can rewrite the infix expression 12
  • As the postfix expression 1 2
  • Think like a computer
  • load value 1 into accumulator
  • load value 2 into register A
  • Add value in register A to value in accumulator
  • How about 1234 ?
  • How about 234?
  • How about 234?

12
How to implement?
  • Can you write method that evaluates postfix
    expressions?
  • double postfixeval(Object items)
  • Where objects in items are either
  • Double
  • Character

13
Postfix evaluation using a stack
  • Make an empty stack
  • Read tokens until EOF
  • If operand push onto stack
  • If operator
  • Pop two stack values
  • Perform binary operation
  • Push result
  • At EOF, pop final result

14
Trace by hand
  • 1 2 3 4
  • 2 3 4
  • 2 3 4

15
Stacks to manage function calls
  • Especially for recursion
  • Fibonacci numbers
  • fib(1) fib(2) 1
  • fib(n) fib(n-1) fib(n-2)
  • int fib (int n)
  • if (n lt 3) return 1
  • else return fib (n-1) fib(n-2)

16
Rewrite fib
  • int fib (int n)
  • int temp1, temp2
  • if (n lt 3) return 1
  • else
  • temp1 fib (n-1)
  • temp2 fib(n-2)
  • return temp1 temp2

17
fib(5)
fib(2)
1
fib(3)
fib(4)
fib(5)
18
fib(5)
fib(1)
1
fib(3) 1
fib(4)
fib(5)
19
fib(5)
fib(3) 1 1
2
fib(4)
fib(5)
20
fib(5)
fib(2)
1
fib(4) 2
fib(5)
21
fib(5)
fib(4) 2 1
3
fib(5)
22
fib(5)
fib(2)
1
fib(3)
fib(5) 3
23
fib(5)
fib(1)
1
fib(3) 1
fib(5) 3
24
fib(5)
fib(3) 1 1
2
fib(5) 3
25
fib(5)
  • fib(5) returns fib(4) fib(3) 3 2 5

fib(5) 3 2
26
fib(5)
fib(1)
fib(2)
1
1
fib(3)
fib(3)
fib(3)
2
fib(5)
fib(5)
fib(5)
27
fib(5)
fib(2)
fib(1)
1
1
fib(3)
fib(3)
fib(3)
fib(2)
2
1
fib(4)
fib(4)
fib(4)
fib(4)
fib(4)
3
fib(5)
fib(5)
fib(5)
fib(5)
fib(5)
28
Queues
  • FIFO (first in, first out) data structure
  • Restricts access to the least recently inserted
    item
  • Queues ADT operations enqueue dequeue
  • void enqueue(Object o)
  • Object dequeue()
  • Object getfront()

29
QUEUES
enqueue
dequeue
getFront
30
QUEUE INTERFACE
  • public interface Queue
  • void enqueue(Object x) //insert
  • Object getFront() // find
  • Object dequeue() // find remove
  • boolean isEmpty()
  • void makeEmpty()
Write a Comment
User Comments (0)
About PowerShow.com