1 / 9

Chapter 6: Stacks

Chapter 6: Stacks. Stack Applications. Stack Implementations. CS 240. 35. 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.

chione
Download Presentation

Chapter 6: 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. Chapter 6: Stacks Stack Applications Stack Implementations CS 240 35

  2. 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 36

  3. 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; } u: 10 u: 20 u: 30 u: 20 v: 20 v: 30 v: 10 v: 20 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. temp:30 temp: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 (c <= b) swap(b,c); } a: 10 a: 20 b: 20 b: 30 c: 30 c: 20 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 x: 10 y: 20 y: 30 z: 10 z: 30 z: 20 CS 240 37

  4. 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 / + * - CS 240 38

  5. 2 8 8 4 5 5 5 5 9 3 6 3 3 3 3 3 27 13 13 7 13 13 13 13 13 Stack Application: Postfix Expression Evaluation Following these rules, then, with the postfix expression 7 6 + 3 5 8 2 / + * - yields: 7 13 -14 CS 240 39

  6. translate scale scale rotate rotate translate translate rotate translate translate Stack Application: Graphical Transformations translate 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. scale rotate By carefully applying the transformations in the correct order (via the stack), the image is altered in the desired fashion. translate CS 240 40

  7. c b b b a a a b b a a b a c 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 CS 240 41

  8. Linked List Implementation of Stack // Class declaration file: stack.h // Linked List implementation of the // stack ADT – inherits from LinkedList. #ifndefSTACK_H #include "LinkedList.h" class stack : protected LinkedList { public: // Class constructors stack(); stack(conststack &s); // Member functions boolisEmpty(); void push(constelementType &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. 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! CS 240 42

  9. // Class implementation file: stack.cpp // Linked List implementation of the // stack ADT – inherits from LinkedList. #include "Stack.h" #include "LinkedList.h" #include <assert.h> // 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. // boolstack:: isEmpty() { return head == NULL; } // Push function: inserts item at // // the top of the stack. // void stack:: push(constelementType &elt) { nodePtrnewHead = 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() { elementTypeelt; nodePtroldHead; 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() { elementTypeelt; assert(head != NULL); elt = head->item; return elt; } CS 240 43

More Related