Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linear Data Structures LIFO – Polish notation Context Saving.

Similar presentations


Presentation on theme: "Linear Data Structures LIFO – Polish notation Context Saving."— Presentation transcript:

1 Linear Data Structures LIFO – Polish notation Context Saving

2 Arithmetic Expression Infix: 1+2x3+1/2 = ? ((1+2)x(3+1))/2 = 6 (1+(2x3)+1)/2 = 4 (1+(2x3))+(1/2) = 7.5 Postfix (Polish notation): Let A, B, be operands, ♦ an operator. Instead of A♦B, write AB♦

3 Arithmetic Expression (2) Example: Infix: ((1+2)x(3+1))/2 Postfix: ((1+2)x(3+1))2/ (1+2)(3+1)x2/ (1+2)31+x2/ 12+31+x2/ Infix: (1+(2x3))+(1/2) Postfix: (1+(2x3))(1/2)+ (1+(2x3))12/+ 1(2x3)+12/+ 123x+12/+

4 Arithmetic Expression (3) Advantage of Polish Notation: No ambiguity! A-B-C =? (A-B)-C A-(B-C) AB-C- ABC--

5 Arithmetic Expression (4) But how do we read it? Can the Polish people really understand this? 12+31+x2/

6 Arithmetic Expression (5) Algorithm for Computing Postfix Arithmetic Expression: Data Structures: A[1..n] - a legal arithmetic expression in postfix notation. E - a stack of evaluated parts of the expression. Output: Top of stack.

7 Arithmetic Expression (6) The Algorithm For i=1 to n do If A[i] is not an operator then PUSH(E,A[i]) else POP(E,X) POP(E,Y) PUSH(E,Y A[i] X) (remember:A[i] is operator) endFor POP(E,Solution)

8 Arithmetic Expression (7) Example: 12+31+x2/ ((1+2)x(3+1))/2=6 1 2 operator 21+=3 3

9 Arithmetic Expression (7) Example: 12+31+x2/ ((1+2)x(3+1))/2=6 3 1 operator 13+=4 3 4

10 Arithmetic Expression (7) Example: 12+31+x2/ ((1+2)x(3+1))/2=6 operator 43x=12 3 4 12 2

11 Arithmetic Expression (7) Example: 12+31+x2/ ((1+2)x(3+1))/2=6 operator 212/=6 12 2 6 6

12 Arithmetic Expression (8) Exercise: Use LIFO to write a simple algorithm that recognizes whether an input array is a legal arithmetic expression in postfix notation.

13 Context Saving Telephone switches Then… Now: digital. Huge programs, thousands of lines of code. No loops, no functions.

14 Context Saving (2) Software: Loops: Subroutines: Variable I A CALL B B CALL C C CALL A

15 Context Saving (3) Software: Recursive Subroutines: Example: Sudoku. Context saving: LIFO. A CALL A


Download ppt "Linear Data Structures LIFO – Polish notation Context Saving."

Similar presentations


Ads by Google