Linear Data Structures LIFO Polish notation Context Saving

  • Slides: 15
Download presentation
Linear Data Structures LIFO – Polish notation Context Saving

Linear Data Structures LIFO – Polish notation Context Saving

Arithmetic Expression Infix: 1+2 x 3+1/2 = ? (1+(2 x 3)+1)/2 (1+(2 x 3))+(1/2)

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

Arithmetic Expression (2) Example: Infix: ((1+2)x(3+1))/2 Infix: (1+(2 x 3))+(1/2) Postfix: ((1+2)x(3+1))2/ (1+2)(3+1)x 2/

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

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

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

Arithmetic Expression (4) But how do we read it? Can the Polish people really

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

Arithmetic Expression (5) Algorithm for Computing Postfix Arithmetic Expression: Data Structures: A[1. . n]

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.

Arithmetic Expression (6) The Algorithm For i=1 to n do If A[i] is not

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) end. For POP(E, Solution)

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

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

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

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

Arithmetic Expression (7) Example: 12+31+x 2/ operator 3 x 4 =12 4 2 12

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

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

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

Arithmetic Expression (8) Exercise: Use LIFO to write a simple algorithm that recognizes whether

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.

Context Saving Telephone switches Then… Now: digital. Huge programs, thousands of lines of code.

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

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

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

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

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