# Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS 240 35.

## Presentation on theme: "Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS 240 35."— Presentation transcript:

Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS 240 35

CS 240 36 The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding and removing elements. The principal stack operations: Create an empty stack. Copy an existing stack. Destroy a stack. Determine whether a stack is empty. Add a new element to a stack. Remove the most recently added element from a stack. Retrieve the most recently added element from a stack.

CS 240 37 When running a program that uses functions, a stack is used to keep track of the function calls, including the status of the variables in each function. Stack Application: Run-Time Stack void swap(int &u, int &v) { int temp = u; u = v; v = temp; } void swap(int &u, int &v) { int temp = u; u = v; v = temp; } void reorder(int &a, int &b, int &c) { if ((b <= a) && (b <= c)) swap(a,b); else if ((c <= a) && (c <= b)) swap(a,c); if (c <= b) swap(b,c); } void reorder(int &a, int &b, int &c) { if ((b <= a) && (b <= c)) swap(a,b); else if ((c <= a) && (c <= b)) swap(a,c); if (c <= b) swap(b,c); } a: 20 b: 30 c: 10 void main() { int x = 20; int y = 30; int z = 10; if ((x > y) || (y > z)) reorder(x,y,z); cout << x << y << z; } void main() { int x = 20; int y = 30; int z = 10; if ((x > y) || (y > z)) reorder(x,y,z); cout << x << y << z; } u: 20 v: 10 temp:20 x: 20 y: 30 z: 10 u: 10 v: 20 a: 10 c: 20 x: 10 z: 20 void swap(int &u, int &v) { int temp = u; u = v; v = temp; } void swap(int &u, int &v) { int temp = u; u = v; v = temp; } u: 30 v: 20 temp:30 u: 20 v: 30 b: 20 y: 20 c: 30 z: 30

CS 240 38 Stack Application: Infix-to-Postfix Conversion Following these rules, then, the infix expression 7 + 6 - 3 * ( 5 + 8 / 2 ) is converted into the postfix expression 7 6 + 3 5 8 2 / + * - When this is found in the infix expression... … do this! Beginning of infix expression Push a # onto the stack Operand Append it to the postfix expression Right parenthesis Repeatedly pop the stack, appending each entry to the postfix expression, until a left parenthesis is popped (but not output) End of infix expression Repeatedly pop the stack, appending each entry to the postfix expression Left parenthesis Push it onto the stack * or / operator Repeatedly pop the stack, appending all popped * and / operators to the end of the postfix expression, until something else is popped; push this last item back onto the stack, followed by the new * or / that was encountered + or - operator Repeatedly pop the stack, appending all popped +, -, *, and / operators to the end of the postfix expression, until something else is popped; push this last item back onto the stack, followed by the new + or - that was encountered

CS 240 39 Stack Application: Postfix Expression Evaluation Following these rules, then, with the postfix expression 7 6 + 3 5 8 2 / + * - yields: When this is encountered in the postfix expression... … do this! Operand Push it onto the stack Operator Pop the stack twice, perform the operation on the two popped operands, and push the result back onto the stack End of postfix expression Pop the stack once; the popped value is the final result

CS 240 40 Stack Application: Graphical Transformations When graphically manipulating 2D and 3D objects, it’s often convenient to use a stack to manipulate them at the origin and then translate them to their appropriate locations. translate rotate scale translate rotate scale translate rotate translate By carefully applying the transformations in the correct order (via the stack), the image is altered in the desired fashion. translate scale rotate translate

CS 240 41 Stack Implementation Alternatives An Array Implementation Positives Avoids pointers (uses top index) Trivial implementation  Negatives × Size must be declared in advance A Linked List Implementation Positives Dynamically allocates exactly the right amount of memory Straightforward (if not quite trivial) implementation  Negatives × Those wonderful pointers           