780 likes | 1.12k Views
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.
E N D
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
Stacks • A stack is a sequence of items that are accessible at only one end of the sequence. Stacks
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
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
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
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
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
STACK APPLICATIONS Stacks
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
STACK APPLICATION HOW COMPILERS IMPLEMENT RECURSION Stacks
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
THERE IS A RUN-TIME STACK TO HANDLE THESE ACTIVATION RECORDS. PUSH: WHEN FUNCTION IS CALLED POP: WHEN EXECUTION OF FUNCTION IS COMPLETED Stacks
AN ACTIVATION RECORD IS SIMILAR TO AN EXECUTION FRAME, EXCEPT THAT AN ACTIVATION RECORD HAS VARIABLES ONLY, NO CODE. Stacks
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
STACK APPLICATION CONVERTING FROM INFIX TO POSTFIX Stacks
IN INFIX NOTATION, AN OPERATOR IS PLACED BETWEEN ITS OPERANDS. a + b c – d + (e * f – g * h) / i Stacks
OLD COMPILERS: INFIX MACHINE LANGUAGE THIS GETS MESSY BECAUSE OF PARENTHESES. NEWER COMPILERS: INFIX POSTFIX MACHINE LANG. Stacks
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
PARENTHESES ARE NOT NEEDED, AND NOT USED, IN POSTFIX. Stacks
LET’S CONVERT AN INFIX STRING TO A POSTFIX STRING. x – y * z Stacks
POSTFIX PRESERVES THE ORDER OF OPERANDS, SO AN OPERAND CAN BE APPENDED TO POSTFIX AS SOON AS THAT OPERAND IS ENCOUNTERED IN INFIX. Stacks
INFIX POSTFIX x – y * z x Stacks
INFIX POSTFIX x – y * z x THE OPERANDS FOR ‘-’ ARE NOT YET IN POSTFIX, SO ‘-’ MUST BE TEMPORARILY SAVED SOMEWHERE. (STACK) Stacks
INFIX POSTFIX x – y * z xy Stacks
INFIX POSTFIX x – y * z xy THE OPERANDS FOR ‘*’ ARE NOT YET IN POSTFIX, SO ‘*’ MUST BE TEMPORARILY SAVED SOMEWHERE, AND RESTORED BEFORE ‘-’. Stacks
INFIX POSTFIX x – y * z xyz Stacks
INFIX POSTFIX x – y * z xyz* – Stacks
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
THE TEMPORARY STORAGE FACILITY IS A STACK. HERE IS THE STRATEGY FOR MAINTAINING THE STACK: Stacks
INFIX GREATER, PUSH Stacks
CONVERT FROM INFIX TO POSTFIX: INFIX POSTFIX a + b * c / d - e Stacks
INFIX POSTFIX a + b * c / d – e abc*d/+e – - / * + OPERATOR STACK Stacks
CONVERT TO POSTFIX: x * (y + z) Stacks