Download presentation
Presentation is loading. Please wait.
Published byColleen Ryan Modified over 9 years ago
2
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4
3
Agenda Stack Definition Implementation Analysis Queue Definition Implementation Analysis
4
Workshop sign-up Still time : Dave Feil-Seifer df001i@mail.rochester.edu Ross Carmara rc001i@mail.rochester.edurc001i@mail.rochester.edu Read Culotta article on team work in science
5
HOMEWORK 2 A&U 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
6
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()
7
STACK push pop, top
8
STACK INTERFACE public interface Stack{ void push(Object x); //insert void pop(); // remove Object top(); // “find” Object topAndPop(); // find + remove boolean isEmpty(); void makeEmpty(); }
9
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?
10
A useful stack algorithm Balance checking 1. Make an empty stack 2. Read symbols until the end of file a. If the token is an opening symbol, push it onto the stack b. If the token is a closing symbol i. If the stack is empty, report an error ii. Otherwise, pop the stack, if the symbol is not the corresponding opening symbol report an error 3. At EOF if stack is not empty, report an error
11
Trace by hand [(({})]) ([]})[
12
A useful stack algorithm Postfix evaluation We can rewrite the infix expression 1+2 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 1+2+3+4 ? How about 2*3+4? How about 2+3*4?
13
How to implement? Can you write method that evaluates postfix expressions? double postfixeval(Object[] items) Where objects in items[] are either Double Character
14
Postfix evaluation using a stack 1. Make an empty stack 2. Read tokens until EOF a. If operand push onto stack b. If operator i. Pop two stack values ii. Perform binary operation iii. Push result 3. At EOF, pop final result
15
Trace by hand 1 2 3 4 + + + 2 3 * 4 + 2 3 4 * +
16
Stacks to manage function calls EEspecially for recursion FFibonacci numbers ffib(1) == fib(2) == 1 ffib(n) = fib(n-1) + fib(n-2) int fib (int n){ if (n < 3) return 1; else return fib (n-1) + fib(n-2); }
17
Rewrite fib int fib (int n){ int temp1, temp2 if (n < 3) return 1; else { temp1 = fib (n-1) ; temp2 = fib(n-2); return temp1 + temp2; }
18
fib(5) fib(4) fib(2) fib(3) 1
19
fib(5) fib(4) fib(3) = 1 + fib(1) 1
20
fib(5) fib(4) fib(3) = 1 + 1 2
21
fib(5) fib(4) = 2 + fib(2) 1
22
fib(5) fib(4) = 2 + 1 3
23
fib(5) fib(5) = 3 + fib(3) fib(2) 1
24
fib(5) fib(5) = 3 + fib(3) = 1 + fib(1) 1
25
fib(5) fib(5) = 3 + fib(3) = 1 + 1 2
26
fib(5) fib(5) returns fib(4) + fib(3) == 3 + 2 == 5 fib(5) = 3 + 2
27
fib(5) fib(3) fib(2) 1 fib(5) fib(3) fib(1) 1 fib(5) fib(3) 2
28
fib(5) fib(4) fib(2) fib(3) 1 fib(5) fib(4) fib(3) fib(1) 1 fib(5) fib(4) fib(3) 2 fib(5) fib(4) fib(2) 1 fib(5) fib(4) 3
29
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()
30
QUEUES enqueuedequeue getFront
31
QUEUE INTERFACE public interface Queue{ void enqueue(Object x); //insert Object getFront(); // “find” Object dequeue(); // find + remove boolean isEmpty(); void makeEmpty(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.