Topic 15 I Implementing and Using Stacks l ti d U i St k - - PowerPoint PPT Presentation

topic 15 i implementing and using stacks l ti d u i st k
SMART_READER_LITE
LIVE PREVIEW

Topic 15 I Implementing and Using Stacks l ti d U i St k - - PowerPoint PPT Presentation

Topic 15 I Implementing and Using Stacks l ti d U i St k "stack n. The set of things a person has to do in the future. "I haven't Th t f thi h t d i th f t "I h 't done it yet because every time I pop my stack


slide-1
SLIDE 1

Topic 15 I l ti d U i St k Implementing and Using Stacks

"stack n. Th t f thi h t d i th f t "I h 't The set of things a person has to do in the future. "I haven't done it yet because every time I pop my stack something new gets pushed." If you are interrupted several times in the g p y p middle of a conversation, "My stack overflowed" means "I forget what we were talking about."

  • The Hacker's Dictionary

Friedrich L Bauer Friedrich L. Bauer

German computer scientist who proposed "stack method

  • f expression evaluation"

CS 307 Fundamentals of Computer Science Stacks

1

  • f expression evaluation

in 1955.

slide-2
SLIDE 2

Stack Overflow

CS 307 Fundamentals of Computer Science Stacks

2

slide-3
SLIDE 3

Sharper Tools Lists Stacks Lists

CS 307 Fundamentals of Computer Science Stacks

3

slide-4
SLIDE 4

Stacks

8Access is allowed only at one point of the structure 8Access is allowed only at one point of the structure, normally termed the top of the stack

– access to the most recently added item only – access to the most recently added item only

8 Operations are limited:

– push (add item to stack) push (add item to stack) – pop (remove top item from stack) – top (get top item without removing it) p (g p g ) – clear – isEmpty – size?

8Described as a "Last In First Out" (LIFO) d t t t

CS 307 Fundamentals of Computer Science Stacks

4

(LIFO) data structure

slide-5
SLIDE 5

Stack Operations

Assume a simple stack for integers. Stack s = new Stack(); s.push(12); s push(4); s.push(4); s.push( s.top() + 2 ); () s.pop() s.push( s.top() ); //what are contents of stack?

CS 307 Fundamentals of Computer Science Stacks

5

slide-6
SLIDE 6

Stack Operations

Write a method to print out contents of stack in reverse order.

CS 307 Fundamentals of Computer Science Stacks

6

slide-7
SLIDE 7

Common Stack Error

Stack s = new Stack(); // put stuff in stack for(int i 0; i < 5; i++) for(int i = 0; i < 5; i++) s.push( i ); // print out contents of stack // print out contents of stack // while emptying it. (??) for(int i = 0; i < s.size(); i++) System.out.print( s.pop() + “ “); // Wh t i t t? // What is output?

CS 307 Fundamentals of Computer Science Stacks

7

slide-8
SLIDE 8

Attendance Question 1

8What is output of code on previous slide? A 0 1 2 3 4 B 4 3 2 1 0 C 4 3 2 C 4 3 2 D 2 3 4 E N d i E No output due to runtime error.

CS 307 Fundamentals of Computer Science Stacks

8

slide-9
SLIDE 9

Corrected Version

Stack s = new Stack(); // put stuff in stack for(int i = 0; i < 5; i++) for(int i = 0; i < 5; i++) s.push( i ); // print out contents of stack p // while emptying it int limit = s.size(); for(int i = 0; i < limit; i++) System.out.print( s.pop() + “ “); // //or // while( !s.isEmpty() )

CS 307 Fundamentals of Computer Science Stacks

9

// System.out.println( s.pop() );

slide-10
SLIDE 10

Implementing a stack

8 d d l i ll i h ld h l 8need an underlying collection to hold the elements

  • f the stack

82 b i h i 82 basic choices

– array (native or ArrayList) linked list – linked list

8array implementation 8linked list implementation 8Some of the uses for a stack are much more interesting than the implementation of a stack

CS 307 Fundamentals of Computer Science Stacks

10

interesting than the implementation of a stack

slide-11
SLIDE 11

Applications of Stacks Applications of Stacks

CS 307 Fundamentals of Computer Science Stacks

11

slide-12
SLIDE 12

Problems that Use Stacks

8The runtime stack used by a process (running program) to keep track of methods in progress 8Search problems 8Undo, redo, back, forward U do, edo, bac , o a d

CS 307 Fundamentals of Computer Science Stacks

12

slide-13
SLIDE 13

Mathematical Calculations

Wh t i 3 2 * 4? 2 * 4 3? 3 * 2 4? What is 3 + 2 * 4? 2 * 4 + 3? 3 * 2 + 4? The precedence of operators affects the d f ti A th ti l

  • rder of operations. A mathematical

expression cannot simply be evaluated left to right right. A challenge when evaluating a program. L i l l i i th f Lexical analysis is the process of interpreting a program. I l T k i ti Involves Tokenization Wh t b t 1 2 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 3

CS 307 Fundamentals of Computer Science Stacks

13

What about 1 - 2 - 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 3

slide-14
SLIDE 14

Infix and Postfix Expressions

8Th t iti 8The way we are use to writing expressions is known as infix notation notation 8Postfix expression does not 8 i d l 8require any precedence rules 83 2 * 1 + is postfix of 3 * 2 + 1 8evaluate the following postfix expressions and write out a di i fi i corresponding infix expression:

2 3 2 4 * + * 1 2 3 4 ^ * + 1 2 3 2 ^ 3 * 6 / + 2 5 ^ 1

CS 307 Fundamentals of Computer Science Stacks

14

1 2 - 3 2 ^ 3 * 6 / + 2 5 ^ 1 -

slide-15
SLIDE 15

Attendance Question 2

8What does the following postfix expression evaluate to? 6 3 2 + *

  • A. 18
  • B. 36

C 24

  • C. 24
  • D. 11
  • E. 30

CS 307 Fundamentals of Computer Science Stacks

15

slide-16
SLIDE 16

Evaluation of Postfix Expressions

8E t d ith t k 8Easy to do with a stack 8given a proper postfix expression:

– get the next token – if it is an operand push it onto the stack – else if it is an operator

  • pop the stack for the right hand operand
  • pop the stack for the left hand operand
  • apply the operator to the two operands

h th lt t th t k

  • push the result onto the stack

– when the expression has been exhausted the result is the top (and only element) of the stack

CS 307 Fundamentals of Computer Science Stacks

16

result is the top (and only element) of the stack

slide-17
SLIDE 17

Infix to Postfix

8Convert the following equations from infix to postfix:

2 ^ 3 ^ 3 + 5 * 1 11 + 2 - 1 * 3 / 3 + 2 ^ 2 / 3 Problems: Negative numbers? parentheses in expression

CS 307 Fundamentals of Computer Science Stacks

17

slide-18
SLIDE 18

Infix to Postfix Conversion

8R i t d i l ith 8Requires operator precedence parsing algorithm

– parse v. To determine the syntactic structure of a sentence or other utterance

Operands: add to expression Close parenthesis: pop stack symbols until an open parenthesis appears Operators: Have an on stack and off stack precedence Pop all stack symbols until a symbol of lower precedence appears Then push the operator precedence appears. Then push the operator End of input: Pop all remaining stack symbols and add to the expression

CS 307 Fundamentals of Computer Science Stacks

18

add to the expression

slide-19
SLIDE 19

Simple Example

Infix Expression: 3 + 2 * 4 Infix Expression: 3 + 2 4 PostFix Expression: Operator Stack: Operator Stack: Precedence Table

Symbol Off Stack On Stack Precedence Precedence + 1 1 + 1 1

  • 1

1 * 2 2 / 2 2 / 2 2 ^ 10 9 ( 20

CS 307 Fundamentals of Computer Science Stacks

19

slide-20
SLIDE 20

Simple Example

Infix Expression: + 2 * 4 Infix Expression: + 2 4 PostFix Expression: 3 Operator Stack: Operator Stack: Precedence Table

Symbol Off Stack On Stack Precedence Precedence + 1 1 + 1 1

  • 1

1 * 2 2 / 2 2 / 2 2 ^ 10 9 ( 20

CS 307 Fundamentals of Computer Science Stacks

20

slide-21
SLIDE 21

Simple Example

Infix Expression: 2 * 4 Infix Expression: 2 4 PostFix Expression: 3 Operator Stack: + Operator Stack: + Precedence Table

Symbol Off Stack On Stack Precedence Precedence + 1 1 + 1 1

  • 1

1 * 2 2 / 2 2 / 2 2 ^ 10 9 ( 20

CS 307 Fundamentals of Computer Science Stacks

21

slide-22
SLIDE 22

Simple Example

Infix Expression: * 4 Infix Expression: 4 PostFix Expression: 3 2 Operator Stack: + Operator Stack: + Precedence Table

Symbol Off Stack On Stack Precedence Precedence + 1 1 + 1 1

  • 1

1 * 2 2 / 2 2 / 2 2 ^ 10 9 ( 20

CS 307 Fundamentals of Computer Science Stacks

22

slide-23
SLIDE 23

Simple Example

Infix Expression: 4 Infix Expression: 4 PostFix Expression: 3 2 Operator Stack: + * Operator Stack: + Precedence Table

Symbol Off Stack On Stack Precedence Precedence + 1 1 + 1 1

  • 1

1 * 2 2 / 2 2 / 2 2 ^ 10 9 ( 20

CS 307 Fundamentals of Computer Science Stacks

23

slide-24
SLIDE 24

Simple Example

Infix Expression: Infix Expression: PostFix Expression: 3 2 4 Operator Stack: + * Operator Stack: + Precedence Table

Symbol Off Stack On Stack Precedence Precedence + 1 1 + 1 1

  • 1

1 * 2 2 / 2 2 / 2 2 ^ 10 9 ( 20

CS 307 Fundamentals of Computer Science Stacks

24

slide-25
SLIDE 25

Simple Example

Infix Expression: Infix Expression: PostFix Expression: 3 2 4 * Operator Stack: + Operator Stack: + Precedence Table

Symbol Off Stack On Stack Precedence Precedence + 1 1 + 1 1

  • 1

1 * 2 2 / 2 2 / 2 2 ^ 10 9 ( 20

CS 307 Fundamentals of Computer Science Stacks

25

slide-26
SLIDE 26

Simple Example

Infix Expression: Infix Expression: PostFix Expression: 3 2 4 * + Operator Stack: Operator Stack: Precedence Table

Symbol Off Stack On Stack Precedence Precedence + 1 1 + 1 1

  • 1

1 * 2 2 / 2 2 / 2 2 ^ 10 9 ( 20

CS 307 Fundamentals of Computer Science Stacks

26

slide-27
SLIDE 27

Example

1 - 2 ^ 3 ^ 3 - ( 4 + 5 * 6 ) * 7 Show algorithm in action on above equation

CS 307 Fundamentals of Computer Science Stacks

27

slide-28
SLIDE 28

Balanced Symbol Checking

8In processing programs and working with computer languages there are many instances when symbols must be balanced

{ } , [ ] , ( ) A stack is useful for checking symbol balance. When a closing symbol is found it must match the most recent opening symbol of the same t pe type.

Algorithm?

CS 307 Fundamentals of Computer Science Stacks

28

slide-29
SLIDE 29

Algorithm for Balanced Symbol Checking Symbol Checking

8Make an empty stack 8read symbols until end of file

– if the symbol is an opening symbol push it onto the stack – if it is a closing symbol do the following

  • if the stack is empty report an error
  • otherwise pop the stack. If the symbol popped does

not match the closing symbol report an error not match the closing symbol report an error

8At the end of the file if the stack is not empty report an error

CS 307 Fundamentals of Computer Science Stacks

29

report an error

slide-30
SLIDE 30

Algorithm in practice

8li [i] 3 * ( 44 h d( f ( li [ 2 * (i 1) f ( 8list[i] = 3 * ( 44 - method( foo( list[ 2 * (i + 1) + foo( list[i - 1] ) ) / 2 * ) - list[ method(list[0])]; 8Complications

h i it t t h t hi b l ? – when is it not an error to have non matching symbols?

8Processing a file 8Processing a file

– Tokenization: the process of scanning an input stream. Each independent chunk is a token. p

8Tokens may be made up of 1 or more characters

CS 307 Fundamentals of Computer Science Stacks

30