Presentation is loading. Please wait.

Presentation is loading. Please wait.

CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.

Similar presentations


Presentation on theme: "CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1."— Presentation transcript:

1 CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1

2 Quick Recap  Stacks are linear lists.  All deletions and insertions occur at one end of the stack known as the TOP.  Data going into the stack first, leaves out last.  Stacks are also known as LIFO data structures (Last- In, First-Out). 2

3 Basic Stack Operations  push – Adds an item to the top of a stack.  pop – Removes an item from the top of the stack and returns it to the user.  stack top (top, peek) – Copies the top item of the stack and returns it to the user; the item is not removed, hence the stack is not altered. 3

4 Additional Notes  Stacks structures are usually implemented using arrays or linked lists.  For both implementations, the running time is O(n).  We will be examining common Stack Applications. 4

5 5 Uses of Stacks  The runtime stack used by a process (running program) to keep track of methods in progress  Undo, redo, back, forward

6

7 Search Backtracking  Stacks can be used to backtrack to achieve certain goals.  Usually, we set up backtrack tokens to indicate a backtrack opportunity. 7

8 Stack Applications  Reversing Data: We can use stacks to reverse data. (example: files, strings) Very useful for finding palindromes. Consider the following pseudocode: 1)read (data) 2)loop (data not EOF and stack not full) 1) push (data) 2) read (data) 3)Loop (while stack notEmpty) 1) pop (data) 2) print (data) 8

9 Stack Applications  Converting Decimal to Binary: Consider the following pseudocode 1) Read (number) 2) Loop (number > 0) 1) digit = number modulo 2 2) print (digit) 3) number = number / 2 // from Data Structures by Gilbert and Forouzan The problem with this code is that it will print the binary number backwards. (ex: 19 becomes 11001000 instead of 00010011. ) To remedy this problem, instead of printing the digit right away, we can push it onto the stack. Then after the number is done being converted, we pop the digit out of the stack and print it. 9

10 10 Balanced Symbol Checking  In 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 type.  Applicable to checking html and xml tags!

11 11 Algorithm for Balanced Symbol Checking  Make an empty stack  read 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  At the end of the file if the stack is not empty report an error

12 12 Algorithm in practice  list[i] = 3 * ( 44 - method( foo( list[ 2 * (i + 1) + foo( list[i - 1] ) ) / 2 * ) - list[ method(list[0])];  Complications  when is it not an error to have non matching symbols?  Processing a file  Tokenization: the process of scanning an input stream. Each independent chunk is a token.  Tokens may be made up of 1 or more characters

13 13 Mathematical Calculations  What does 3 + 2 * 4 equal? 2 * 4 + 3? 3 * 2 + 4?  The precedence of operators affects the order of operations.  A mathematical expression cannot simply be evaluated left to right.  A challenge when evaluating a program.  Lexical analysis is the process of interpreting a program. What about 1 - 2 - 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 3

14 CS314Stacks 14 Infix and Postfix Expressions  The way we are use to writing expressions is known as infix notation  Postfix expression does not require any precedence rules  3 2 * 1 + is postfix of 3 * 2 + 1  evaluate the following postfix expressions and write out a corresponding infix expression: 2 3 2 4 * + *1 2 3 4 ^ * + 1 2 - 3 2 ^ 3 * 6 / +2 5 ^ 1 -

15 Clicker Question 2  What does the following postfix expression evaluate to? 6 3 2 + * A. 18 B. 36 C. 24 D. 11 E. 30 CS314Stacks 15

16 Stack Applications  Postponement: Evaluating arithmetic expressions.  Prefix: + a b  Infix: a + b (what we use in grammar school)  Postfix: a b +  In high level languages, infix notation cannot be used to evaluate expressions. We must analyze the expression to determine the order in which we evaluate it. A common technique is to convert a infix notation into postfix notation, then evaluating it. 16

17 Infix to Postfix Conversion  Rules:  Operands immediately go directly to output  Operators are pushed into the stack (including parenthesis) - compare current operator o 1 to stack top operator o 2 - while ((o 1 <= o 2 and o 1 is left-associative) or (o 1 < o 2 and o 1 is right associative)) - Pop (o 2 ) and check the next o 2 to remain in the loop or exit - Push (o 1 ); - i.e. push o 1 when its precedence is higher than o 2 generally, otherwise, pop o 2 taking into consideration the associativity rules shown above. - Priority 1: * / ^ - Priority 0: + - Open parenthesis are always pushed on the stack. If we encounter a closing parenthesis, pop the stack and print the operators until you see a left parenthesis. Do not output parenthesis. 17

18 Infix to Postfix Example #1 A + B * C - D / E (general example) Infix Stack(bot->top)Postfix a) A + B * C - D / E b) + B * C - D / EA c) B * C - D / E+A d) * C - D / E+A B e) C - D / E+ *A B f) - D / E+ *A B C g) D / E-A B C * + h) / E-A B C * + D i) E- /A B C * + D j)- /A B C * + D E k)A B C * + D E / - 18

19 Infix to Postfix Example #2 A / B ^ C - D (operator precedence example) Infix Stack(bot->top)Postfix a) A / B ^ C - Demptyempty b) / B ^ C - DemptyA c) B ^ C - D/A d) ^ C - D/A B e) C - D/ ^A B f) - D/ ^A B C g) - D/A B C ^ h) - DemptyA B C ^ / i) D-A B C ^ / j) -A B C ^ / D k) emptyA B C ^ / D - 19

20 Infix to Postfix Example #3 A * B - ( C + D ) + E ( parenthesis example) Infix Stack(bot->top)Postfix a) A * B - ( C + D ) + Eemptyempty b) * B - ( C + D ) + EemptyA c) B - ( C + D ) + E*A d) - ( C + D ) + E*A B e) - ( C + D ) + EemptyA B * f) ( C + D ) + E-A B * g) C + D ) + E- (A B * h) + D ) + E- (A B * C i) D ) + E- ( +A B * C j) ) + E- ( +A B * C D k) + E-A B * C D + l) + EemptyA B * C D + - m) E+A B * C D + - n) +A B * C D + - E o) emptyA B * C D + - E + 20

21 Infix to Postfix Example #4 A - B + C ( left to right association) Infix Stack(bot->top)Postfix a) A - B + C emptyempty b) - B + CemptyA c) B + C-A d) + C-A B e) C+A B - f) +A B - C g) emptyA B - C + 21

22 Infix to Postfix Example #5 A ^ B ^ C( right to left association) Infix Stack(bot->top)Postfix a) A ^ B ^ Cemptyempty b) ^ B ^ CemptyA c) B ^ C^A d) ^ C^A B e) C^ ^A B f) ^ ^A B C g) emptyA B C ^ ^ 22

23 23 Evaluation of Postfix Expressions  Easy to do with a stack  given 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 push the result onto the stack  when the expression has been exhausted the result is the top (and only element) of the stack

24 Postfix Evaulation Operand: push Operator: pop 2 operands, do the math, pop result back onto stack 1 2 3 + * PostfixStack( bot -> top ) a) 1 2 3 + * b) 2 3 + *1 c) 3 + *1 2 d) + *1 2 3 e) *1 5 // 5 from 2 + 3 f) 5// 5 from 1 * 5 24

25 Postfix Evaulation For Example #1 Infix: A + B * C - D / E Postfix: A B C * + D E / - Values: A = 1, B = 2, C = 3, D = 4, E = 2 PostfixStack( bot -> top ) a) 1 2 3 * + 4 2 / -empty b) 2 3 * + 4 2 / -1 c) 3 * + 4 2 / -1 2 d) * + 4 2 / -1 2 3 e) + 4 2 / -1 6 // 6 from 2 * 3 f) 4 2 / - 7// 7 from 1 + 6 g) 2 / - 7 4 h) / - 7 4 2 i) - 7 2// 2 from 4 / 2 j) 5// 5 from 7 - 2 25


Download ppt "CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1."

Similar presentations


Ads by Google