1 / 77

Stacks

Stacks. Briana B. Morrison Adapted from Alan Eugenio. Topics. Define Stack APIs Applications Create Hex Number Recursion Converting from Infix to Postfix Evaluating Postfix Two stacks Single stack Implementation Array based Linked list based. Stacks.

maylin
Download Presentation

Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks Briana B. Morrison Adapted from Alan Eugenio

  2. Topics • Define Stack • APIs • Applications • Create Hex Number • Recursion • Converting from Infix to Postfix • Evaluating Postfix • Two stacks • Single stack • Implementation • Array based • Linked list based Stacks

  3. Stacks • A stack is a sequence of items that are accessible at only one end of the sequence. Stacks

  4. Pushing/Popping a Stack • Because a pop removes the item last added to the stack, we say that a stack has LIFO (last-in/first-out) ordering. Stacks

  5. Stacks

  6. Stacks

  7. Stacks

  8. CLASS stack CLASS stack <stack> <stack> Constructor Operations stack(); Create an empty stack bool empty(); const Check whether the stack is empty. Return true if it is empty and false otherwise. Stacks

  9. CLASS stack <stack> Operations void pop(); Remove the item from the top of the stack. Precondition: The stack is not empty. Postcondition: Either the stack is empty or the stack has a new topmost item from a previous push. void push(const T& item); Insert the argument item at the top of the stack. Postcondition: The stack has a new item at the top. Stacks

  10. CLASS stack <stack> Operations int size() const; Return the number of items on the stack. T& top() const; Return a reference to the value of the item at the top of the stack. Precondition: The stack is not empty. const T& top() const; Constant version of top(). Stacks

  11. Stacks

  12. DETERMINE THE OUTPUT FROM THE FOLLOWING: stack<int> my_stack; for (int i = 0; i < 10; i++) my_stack.push (i * i); while (!my_stack.empty()) { cout << my_stack.top() << endl; my_stack.pop(); } // while Stacks

  13. Stacks

  14. Stacks

  15. Stacks

  16. STACK APPLICATIONS Stacks

  17. Applications of Stacks • Direct applications • Page-visited history in a Web browser • Undo sequence in a text editor • Saving local variables when one function calls another, and this one calls another, and so on. • Indirect applications • Auxiliary data structure for algorithms • Component of other data structures Stacks

  18. Using a Stack to Create a Hex Number Stacks

  19. STACK APPLICATION HOW COMPILERS IMPLEMENT RECURSION Stacks

  20. Stacks

  21. EACH ACTIVATION RECORD CONTAINS: 1. A VARIABLE THAT CONTAINS THE RETURN ADDRESS IN THE CALLING METHOD; 2. FOR EACH VALUE FORMAL PARAMETER, A VARIABLE THAT CONTAINS A COPY OF THE ARGUMENT; 3. FOR EACH REFERENCE FORMAL PARAMETER, A VARIABLE THAT CONTAINS THE ADDRESS OF THE ARGUMENT; 4. FOR EACH VARIABLE DEFINED IN THE METHOD’S BLOCK, A VARIABLE THAT CONTAINS A COPY OF THAT DEFINED VARIABLE.  Stacks

  22. THERE IS A RUN-TIME STACK TO HANDLE THESE ACTIVATION RECORDS. PUSH: WHEN FUNCTION IS CALLED POP: WHEN EXECUTION OF FUNCTION IS COMPLETED Stacks

  23. AN ACTIVATION RECORD IS SIMILAR TO AN EXECUTION FRAME, EXCEPT THAT AN ACTIVATION RECORD HAS VARIABLES ONLY, NO CODE. Stacks

  24. C++ Run-time Stack main() { int i = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } • The C++ run-time system keeps track of the chain of active functions with a stack • When a function is called, the run-time system pushes on the stack a frame containing • Local variables and return value • Program counter, keeping track of the statement being executed • When a function returns, its frame is popped from the stack and control is passed to the method on top of the stack bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 Stacks

  25. Stacks

  26. Stacks

  27. STACK APPLICATION CONVERTING FROM INFIX TO POSTFIX Stacks

  28. IN INFIX NOTATION, AN OPERATOR IS PLACED BETWEEN ITS OPERANDS. a + b c – d + (e * f – g * h) / i Stacks

  29. OLD COMPILERS: INFIX MACHINE LANGUAGE THIS GETS MESSY BECAUSE OF PARENTHESES. NEWER COMPILERS: INFIX POSTFIX MACHINE LANG. Stacks

  30. In POSTFIX Notation, An OPERATOR is placed IMMEDIATELY AFTER its OPERANDS. INFIX POSTFIX a + b ab+ a + b * c abc*+ a * b + c ab*c+ (a + b) * c ab+c* Stacks

  31. PARENTHESES ARE NOT NEEDED, AND NOT USED, IN POSTFIX. Stacks

  32. LET’S CONVERT AN INFIX STRING TO A POSTFIX STRING.    x – y * z Stacks

  33. POSTFIX PRESERVES THE ORDER OF OPERANDS, SO AN OPERAND CAN BE APPENDED TO POSTFIX AS SOON AS THAT OPERAND IS ENCOUNTERED IN INFIX. Stacks

  34. INFIX POSTFIX x – y * z x Stacks

  35. INFIX POSTFIX x – y * z x THE OPERANDS FOR ‘-’ ARE NOT YET IN POSTFIX, SO ‘-’ MUST BE TEMPORARILY SAVED SOMEWHERE. (STACK) Stacks

  36. INFIX POSTFIX x – y * z xy Stacks

  37. INFIX POSTFIX x – y * z xy THE OPERANDS FOR ‘*’ ARE NOT YET IN POSTFIX, SO ‘*’ MUST BE TEMPORARILY SAVED SOMEWHERE, AND RESTORED BEFORE ‘-’. Stacks

  38. INFIX POSTFIX x – y * z xyz Stacks

  39. INFIX POSTFIX x – y * z xyz* – Stacks

  40. Suppose, instead, we started with x*y-z After moving ‘x’ to postfix, ‘*’ is temporarily saved, and then ‘y’ is appended to postfix. What happens when ‘-’ is accessed? INFIX POSTFIX x * y – z xy Stacks

  41. Stacks

  42. THE TEMPORARY STORAGE FACILITY IS A STACK. HERE IS THE STRATEGY FOR MAINTAINING THE STACK: Stacks

  43. Stacks

  44. INFIX GREATER, PUSH Stacks

  45. CONVERT FROM INFIX TO POSTFIX: INFIX POSTFIX a + b * c / d - e Stacks

  46. INFIX POSTFIX a + b * c / d – e abc*d/+e – - / * + OPERATOR STACK Stacks

  47. Stacks

  48. CONVERT TO POSTFIX: x * (y + z) Stacks

  49. Stacks

  50. Stacks

More Related