1 / 32

CS 132 Spring 2008 Chapter 7 Stacks

CS 132 Spring 2008 Chapter 7 Stacks. Read p 407-460. Problems 1-7. Stacks. Definition: list of homogeneous elements wherein the addition and deletion of elements occur only at one end, called the top of the stack Last In First Out (LIFO)

derek-lloyd
Download Presentation

CS 132 Spring 2008 Chapter 7 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. CS 132 Spring 2008Chapter 7Stacks Read p 407-460. Problems 1-7.

  2. Stacks Definition: • list of homogeneous elements wherein • the addition and deletion of elements • occur only at one end, • called the top of the stack Last In First Out (LIFO) • top element is last element added to the stack • elements added and removed from one end (top) • item added last are removed first Used to implement function calls Used to convert recursive algorithms into nonrecursive algorithms

  3. Empty Stack Stack Operations

  4. Basic Stack Operations initializeStack: initializes the stack to an empty state destroyStack: removes all the elements from the stack, leaving it empty isEmptyStack: checks whether the stack is empty isFullStack: checks whether the stack is full push: add new element to the top of the stack. The input consists of the stack and the new element. Prior to this operation, the stack must exist and must not be full. top: returns the top element of the stack. Prior to this operation, the stack must exist and must not be empty. pop: removes the top element of the stack. Prior to this operation, the stack must exist and must not be empty.

  5. Stack (array implementation)

  6. template<class Type> class stackType { public: const stackType<Type>& operator=(const stackType<Type>&); void initializeStack(); bool isEmptyStack(); bool isFullStack(); void destroyStack(); void push(const Type& newItem); Type top(); void pop(); stackType(int stackSize = 100); stackType(const stackType<Type>& otherStack); ~stackType(); private: int maxStackSize; int stackTop; Type *list; };

  7. Dynamic Arrays (from Chapter 3) Static arrays: the size is fixed at compile time int x[5]; //allocates space for x[0]...x[4] Dynamic arrays: int *p; //declare a pointer to an int p = new int[5];//invoked when you want to use the array. //can be deleted and reinvoked with a different size p points to an int (the first integer in the array created by new) Everything else is the same E.g. to print the array for (int i= 0; i<5; i++) cout << p[i] p p[0] p[1] ....

  8. Empty Stack

  9. initializeStack and destroyStack template<class Type> void stackType<Type>::initializeStack() { stackTop = 0; } template<class Type> void stackType<Type>::destroyStack() { stackTop = 0; }

  10. emptyStack and fullStack template<class Type> bool stackType<Type>::isEmptyStack() { return(stackTop == 0); } template<class Type> bool stackType<Type>::isFullStack() { return(stackTop == maxStackSize); }

  11. Push

  12. Push template<class Type> void stackType<Type>::push(const Type& newItem) { if(!isFullStack()) { list[stackTop] = newItem; //add newItem at the top stackTop++; //increment stackTop } else cerr<<"Cannot add to a full stack." << endl; }

  13. Return Top Element template<class Type> Type stackType<Type>::top() { assert(stackTop != 0); //if the stack is empty, //terminate the program return list[stackTop - 1]; //return the element of the //stack indicated by //stackTop - 1 }

  14. Pop template<class Type> void stackType<Type>::pop() { if(!isEmptyStack()) stackTop--; //decrement stackTop else cerr<<"Cannot remove from an empty stack."<<endl; } Alternate pop: template<class Type> Type stackType<Type>::pop(Type& poppedElem) { if(!isEmptyStack()){ poppedElem = list[stackTop - 1]; stackTop--; } else cerr<<"Cannot remove from an empty stack."<<endl }

  15. Pop

  16. copyStack template<class Type> void stackType<Type>::copyStack(const stackType<Type>& otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; assert(list != NULL); //copy otherStack into this stack for(int j = 0; j < stackTop; j++) list[j] = otherStack.list[j]; }

  17. Copy Constructor template<class Type> stackType<Type>::stackType(const stackType<Type>& otherStack) { list = NULL; copyStack(otherStack); } Overloading the Assignment Operator (=) template<class Type> const stackType<Type>& stackType<Type>::operator= (const stackType<Type>& otherStack) { if(this != &otherStack) //avoid self-copy copyStack(otherStack); return *this; }

  18. Programming Example: Highest GPA • Input a file consisting of each student’s GPA, followed by the name • Output: the highest gpa and the students who received it • Sample data: • 3.8 Lisa • 3.6 John • 3.9 Susan • 3.7 Kathy • 3.4 Jason • 3.9 David • 3.4 Jack //would output 3.9 for Susan and David • Track • highest GPA so far • students who have it • When you reach a higher GPA, discard students on the stack and start over

  19. Highest GPA (Algorithm) while (not end of file) { if (GPA > highestGPA) { destroyStack(stack); push(stack, student name); highestGPA = GPA; } else if(GPA is equal to highestGPA) push(stack, student name); Read the GPA and student name; Output the highest GPA Output the names of the students having the highest GPA

  20. Is There Another Stack Implementation? Yes: linked (see linkedStack.h) Empty linked stack Nonempty linked stack

  21. Push Stack before the push operation Stack and newNode

  22. Push Stack after the statement newNode->link = stackTop; executes Stack after the statement stackTop = newNode; executes

  23. Pop Stack before the popoperation

  24. Pop Stack after the statements temp = stackTop; and stackTop = stackTop->link; execute Stack after the statement delete temp; executes

  25. Application: Postfix Expression Calculator Prefix/Polish Notation: a+b written as +ab Suffix/Postfix/Reverse Polish Notation: a+b written as ab+ Convert to postfix a+ b * c: a + b + c: a/b + c: a/(b + c): (a * b – (c / d) * (e + f)) / (g – h / j): a b c * + a b c + + or a b + c + a b / c + a b c + / a b * c d / e f +*- g h j /- /

  26. Postfix Expression Calculator How do you evaluate 3 + 4 * 2? Assume you can read only one character at a time read the 3, save it read the +, save it read the 4, use with the saved 3 and the +. whoops, 4 needs to have been multiplied by 2 first. Easier: evaluate 3 4 2 * + read the 3, save it read the 4, save it read the 2, save it read the * and multply the two last numbers Where should we save the interim numbers? (a stack)

  27. 3 4 2 * +: 0. Start with an empty stack: |___| 1. Read the 3, save it |_3_| | 4 | 2. Read the 4, save it |_3_| | 2 | | 4 | 3. Read the 2, save it |_3_| | 2 | 4. Read the *, multply the two last numbers | 4 | | 8 | Push result on the stack |_3_| |_3_|

  28. 3 4 2 * + continued: 5. Read the +, add the two last numbers | 8 | Push result on the stack |_3_| |_11_| 6. Pop the final number to get the answer. 7. Check 3 + (4*2) = 3 + 8 = 11! How do we do it in a program? Assume input is followed by = : 3 4 2 * + = To discriminate numbers and operands put # before numbers: #3 #4 #2 * + =

  29. Postfix Expression Calculator (Main Algorithm)

  30. Nonrecursive Algorithm to Print a Linked List Backwards Idea: transfer the list to a stack and then print the stack current = first; //Line 1 while(current != NULL) //Line 2 { stack.push(current); //Line 3 current = current->link; //Line 4 } //print the stack stack.printStack();

  31. Convert a series of characters representing and integer, to the integer E.g. 12345 is 1*104 + 2*103 + 3*102 + 4*101+ 5*100 It is read by the computer as '1' then '2' then '3' '4' '5' and a space Algorithm from before: number = first numeral while (more stuff) read numeral number = 10*number + numeral 1 → 10*1 + 2 → 10 (10*1) + 2) + 3 → 10 (10 (10*1) + 2) + 3) + 4 → 10 (10 (10 (10*1) + 2) + 3) + 4)+ 5 What if there is a decimal part 12345.678? What about -12345.678?

More Related