stack overflow topic 15 implementing and using stacks i l
play

Stack Overflow Topic 15 Implementing and Using Stacks I l ti d - PowerPoint PPT Presentation

Stack Overflow Topic 15 Implementing and Using Stacks I 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


  1. Stack Overflow Topic 15 Implementing and Using Stacks I 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 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 of expression evaluation" of expression evaluation in 1955. CS 307 Fundamentals of CS 307 Fundamentals of 1 2 Computer Science Stacks Computer Science Stacks Sharper Tools Stacks � Access is allowed only at one point of the structure, � Access 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 � 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 ) Stacks – clear Lists Lists – isEmpty – size? � Described as a "Last In First Out" (LIFO) d t (LIFO) data structure t t CS 307 Fundamentals of 3 CS 307 Fundamentals of 4 Computer Science Stacks Computer Science Stacks

  2. Stack Operations Stack Operations Assume a simple stack for integers. Write a method to print out contents of stack in reverse order. 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 CS 307 Fundamentals of 5 6 Computer Science Stacks Computer Science Stacks Common Stack Error Attendance Question 1 � What is output of code on previous slide? Stack s = new Stack(); // put stuff in stack A 0 1 2 3 4 for(int i for(int i = 0; i < 5; i++) 0; i < 5; i++) B 4 3 2 1 0 s.push( i ); // print out contents of stack // print out contents of stack C C 4 3 2 4 3 2 // while emptying it. (??) D 2 3 4 for(int i = 0; i < s.size(); i++) E No output due to runtime error. E N d i System.out.print( s.pop() + “ “); // Wh t i // What is output? t t? CS 307 Fundamentals of 7 CS 307 Fundamentals of 8 Computer Science Stacks Computer Science Stacks

  3. Corrected Version Implementing a stack Stack s = new Stack(); � need an underlying collection to hold the elements � d d l i ll i h ld h l // put stuff in stack of the stack for(int i = 0; i < 5; i++) for(int i = 0; i < 5; i++) � 2 basic choices � 2 b i h i s.push( i ); – array (native or ArrayList) // print out contents of stack p – linked list linked list // while emptying it � array implementation int limit = s.size(); for(int i = 0; i < limit; i++) � linked list implementation System.out.print( s.pop() + “ “); // //or � Some of the uses for a stack are much more // while( !s.isEmpty() ) interesting than the implementation of a stack interesting than the implementation of a stack // System.out.println( s.pop() ); CS 307 Fundamentals of CS 307 Fundamentals of 9 10 Computer Science Stacks Computer Science Stacks Problems that Use Stacks � The runtime stack used by a process (running program) to keep track of methods in progress Applications of Stacks Applications of Stacks � Search problems � Undo, redo, back, forward U do, edo, bac , o a d CS 307 Fundamentals of 11 CS 307 Fundamentals of 12 Computer Science Stacks Computer Science Stacks

  4. Mathematical Calculations Infix and Postfix Expressions � The way we are use to writing � Th Wh t i 3 What is 3 + 2 * 4? 2 * 4 + 3? 3 * 2 + 4? 2 * 4? 2 * 4 3? 3 * 2 4? t iti expressions is known as infix The precedence of operators affects the notation notation order of operations. A mathematical d f ti A th ti l � Postfix expression does not expression cannot simply be evaluated left to right right. � � require any precedence rules i d l A challenge when evaluating a program. � 3 2 * 1 + is postfix of 3 * 2 + 1 L Lexical analysis is the process of i l l i i th f � evaluate the following postfix interpreting a program. expressions and write out a Involves Tokenization I l T k i ti corresponding infix expression: di i fi i 2 3 2 4 * + * 1 2 3 4 ^ * + Wh t What about 1 - 2 - 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 3 b t 1 2 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 3 1 2 - 3 2 ^ 3 * 6 / + 1 2 3 2 ^ 3 * 6 / + 2 5 ^ 1 2 5 ^ 1 - CS 307 Fundamentals of CS 307 Fundamentals of 13 14 Computer Science Stacks Computer Science Stacks Attendance Question 2 Evaluation of Postfix Expressions � Easy to do with a stack � E t d ith t k � What does the following postfix expression evaluate to? � given a proper postfix expression: 6 3 2 + * – get the next token – if it is an operand push it onto the stack A. 18 – else if it is an operator B. 36 • pop the stack for the right hand operand C. 24 C 24 • pop the stack for the left hand operand D. 11 • apply the operator to the two operands • push the result onto the stack h th lt t th t k E. 30 – when the expression has been exhausted the result is the top (and only element) of the stack result is the top (and only element) of the stack CS 307 Fundamentals of 15 CS 307 Fundamentals of 16 Computer Science Stacks Computer Science Stacks

  5. Infix to Postfix Infix to Postfix Conversion � Requires operator precedence parsing algorithm � R i t d i l ith � Convert the following equations from infix to – parse v. To determine the syntactic structure of a postfix: sentence or other utterance 2 ^ 3 ^ 3 + 5 * 1 Operands: add to expression 11 + 2 - 1 * 3 / 3 + 2 ^ 2 / 3 Close parenthesis: pop stack symbols until an open parenthesis appears Problems: Operators: Negative numbers? Have an on stack and off stack precedence parentheses in expression 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 add to the expression CS 307 Fundamentals of CS 307 Fundamentals of 17 18 Computer Science Stacks Computer Science Stacks Simple Example Simple Example Infix Expression: Infix Expression: 3 + 2 4 3 + 2 * 4 Infix Expression: Infix Expression: + 2 4 + 2 * 4 PostFix Expression: PostFix Expression: 3 Operator Stack: Operator Stack: Operator Stack: Operator Stack: Precedence Table Precedence Table Symbol Off Stack On Stack Symbol Off Stack On Stack Precedence Precedence Precedence Precedence + + 1 1 1 1 + + 1 1 1 1 - 1 1 - 1 1 * 2 2 * 2 2 / / 2 2 2 2 / / 2 2 2 2 ^ 10 9 ^ 10 9 ( 20 0 ( 20 0 CS 307 Fundamentals of 19 CS 307 Fundamentals of 20 Computer Science Stacks Computer Science Stacks

  6. Simple Example Simple Example Infix Expression: Infix Expression: 2 4 2 * 4 Infix Expression: Infix Expression: * 4 4 PostFix Expression: 3 PostFix Expression: 3 2 Operator Stack: Operator Stack: + + Operator Stack: Operator Stack: + + Precedence Table Precedence Table Symbol Off Stack On Stack Symbol Off Stack On Stack Precedence Precedence Precedence Precedence + + 1 1 1 1 + + 1 1 1 1 - 1 1 - 1 1 * 2 2 * 2 2 / / 2 2 2 2 / / 2 2 2 2 ^ 10 9 ^ 10 9 ( 20 0 ( 20 0 CS 307 Fundamentals of CS 307 Fundamentals of 21 22 Computer Science Stacks Computer Science Stacks Simple Example Simple Example Infix Expression: Infix Expression: 4 4 Infix Expression: Infix Expression: PostFix Expression: 3 2 PostFix Expression: 3 2 4 Operator Stack: Operator Stack: + * + Operator Stack: Operator Stack: + * + Precedence Table Precedence Table Symbol Off Stack On Stack Symbol Off Stack On Stack Precedence Precedence Precedence Precedence + + 1 1 1 1 + + 1 1 1 1 - 1 1 - 1 1 * 2 2 * 2 2 / / 2 2 2 2 / / 2 2 2 2 ^ 10 9 ^ 10 9 ( 20 0 ( 20 0 CS 307 Fundamentals of 23 CS 307 Fundamentals of 24 Computer Science Stacks Computer Science Stacks

  7. Simple Example Simple Example Infix Expression: Infix Expression: Infix Expression: Infix Expression: PostFix Expression: 3 2 4 * PostFix Expression: 3 2 4 * + Operator Stack: Operator Stack: + + Operator Stack: Operator Stack: Precedence Table Precedence Table Symbol Off Stack On Stack Symbol Off Stack On Stack Precedence Precedence Precedence Precedence + + 1 1 1 1 + + 1 1 1 1 - 1 1 - 1 1 * 2 2 * 2 2 / / 2 2 2 2 / / 2 2 2 2 ^ 10 9 ^ 10 9 ( 20 0 ( 20 0 CS 307 Fundamentals of CS 307 Fundamentals of 25 26 Computer Science Stacks Computer Science Stacks Example Balanced Symbol Checking � In processing programs and working with 1 - 2 ^ 3 ^ 3 - ( 4 + 5 * 6 ) * 7 computer languages there are many Show algorithm in action on above equation 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. t pe Algorithm? CS 307 Fundamentals of 27 CS 307 Fundamentals of 28 Computer Science Stacks Computer Science Stacks

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend