Download presentation

Presentation is loading. Please wait.

Published byKelsey Barks Modified over 2 years ago

2
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks 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.

3
CS 240Chapter 6 - StacksPage 22 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. Example Stack Application #1: The Run-Time Stack void main() { int x = 20; int y = 30; int z = 10; if ((x > y) || (y > z)) reorder(x,y,z); cout << x << y << z; } x: ? y: ? z: ? x: 20 y: 30 z: 10 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 (b <= c) 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; } x: 20 y: 30 z: 10 void swap(int &u, int &v) { int temp = u; u = v; v = temp; } u: 20 v: 10 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 (b <= c) 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; } x: 20 y: 30 z: 10 x: 10 z: 20 u: 10 v: 20 temp: 20 a: 10 c: 20 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 (b <= c) swap(b,c); } a: 10 b: 30 c: 20 void main() { int x = 20; int y = 30; int z = 10; if ((x > y) || (y > z)) reorder(x,y,z); cout << x << y << z; } x: 10 y: 30 z: 20 void swap(int &u, int &v) { int temp = u; u = v; v = temp; } u: 30 v: 20 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 (b <= c) swap(b,c); } a: 10 b: 30 c: 20 void main() { int x = 20; int y = 30; int z = 10; if ((x > y) || (y > z)) reorder(x,y,z); cout << x << y << z; } x: 10 y: 30 z: 20 y: 20 z: 30 u: 20 v: 30 temp: 30 b: 20 c: 30 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 (b <= c) swap(b,c); } a: 10 b: 20 c: 30 void main() { int x = 20; int y = 30; int z = 10; if ((x > y) || (y > z)) reorder(x,y,z); cout << x << y << z; } x: 10 y: 20 z: 30 void main() { int x = 20; int y = 30; int z = 10; if ((x > y) || (y > z)) reorder(x,y,z); cout << x << y << z; } x: 10 y: 20 z: 30

4
CS 240Chapter 6 - StacksPage 23 Example Stack Application #2: Converting Infix To Postfix 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! The beginning of the infix expression Start reading the expression An operandAppend it to the postfix expression A right parenthesisRepeatedly pop the stack, appending each entry to the postfix expression, until a left parenthesis is popped (but not output) A left parenthesisPush it onto the stack A * or / operatorRepeatedly 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 A + or - operatorRepeatedly 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 The end of the infix expression Repeatedly pop the stack, appending each entry to the postfix expression

5
CS 240Chapter 6 - StacksPage 24 Example Stack Application #3: Evaluating A Postfix Expression 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! An operandPush it onto the stack An operatorPop the stack twice, perform the operation on the two popped operands, and push the result back onto the stack The end of the postfix expression Pop the stack once; the popped value is the final result

6
CS 240Chapter 6 - StacksPage 25 Example Stack Application #4: 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 scalerotate translate

7
CS 240Chapter 6 - StacksPage 26 Stack Implementation Alternatives n An Array Implementation –Positives n Avoids pointers (uses top index) n Trivial implementation –Negatives n Size must be declared in advance n A Linked List Implementation –Positives n Dynamically allocates exactly the right amount of memory n Straightforward (if not quite trivial) implementation –Negatives n Those wonderful pointers

8
CS 240Chapter 6 - StacksPage 27 Linked List Implementation of Stack // Class declaration file: stack.h // Linked List implementation of the // stack ADT – inherits from LinkedList. #ifndef STACK_H #include "linkedList.h " #include using namespace std; class stack : protected LinkedList { public: // Class constructors stack(); stack(const stack &s); // Member functions bool isEmpty(); void push(const elementType &item); elementType pop(); elementType retrieve(); }; #define STACK_H #endif The stack class “inherits” from the LinkedList class, so all LinkedList members are accessible to any stack. This derived class has a “protected” access specifier, indicating that the public and protected members of LinkedList are considered protected in the stack class. If the access specifier were “private”, then the public and protected members of LinkedList are considered private in the derived class. If the access specifier were “public”, then the public and protected members of LinkedList are considered public and protected (respectively) in the derived class. The stack class “inherits” from the LinkedList class, so all LinkedList members are accessible to any stack. This derived class has a “protected” access specifier, indicating that the public and protected members of LinkedList are considered protected in the stack class. If the access specifier were “private”, then the public and protected members of LinkedList are considered private in the derived class. If the access specifier were “public”, then the public and protected members of LinkedList are considered public and protected (respectively) in the derived class. Let’s assume that the getNode and head members in LinkedList were declared protected, not private! Let’s also assume that the elementType typedef occurred in the LinkedList definition! Let’s assume that the getNode and head members in LinkedList were declared protected, not private! Let’s also assume that the elementType typedef occurred in the LinkedList definition!

9
CS 240Chapter 6 - StacksPage 28 // Push function: inserts item at // // the top of the stack. // void stack:: push(const elementType &elt) { nodePtr newHead = getNode(elt); assert(newHead != NULL); newHead->next = head; head = newHead; return; } // Pop function: removes and returns the // // top stack entry (if there is one). // elementType stack:: pop() { elementType elt; nodePtr oldHead; assert(head != NULL); oldHead = head; elt = head->item; head = head->next; delete oldHead; return elt; } // On_top function: returns (w/o removing) // // the top stack entry (if there is one). // elementType stack:: retrieve() { elementType elt; assert(head != NULL); elt = head->item; return elt; } // Class implementation file: stack.cpp // Linked List implementation of the // stack ADT – inherits from LinkedList. #include "stack.h" #include "linkedList.h" #include #include using namespace std; // Default constructor: // // Inherited from LinkedList. // stack:: stack(): LinkedList() {} // Copy constructor: // // Inherited from LinkedList. // stack:: stack(const stack &s): LinkedList(s) {} // Empty function: returns a boolean // // value that indicates whether or // // not the stack is empty. // bool stack:: isEmpty() { return head == NULL; }

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google