Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 132 Spring 2008 Chapter 7 Stacks Read p 407-460. Problems 1-7.

Similar presentations


Presentation on theme: "1 CS 132 Spring 2008 Chapter 7 Stacks Read p 407-460. Problems 1-7."— Presentation transcript:

1 1 CS 132 Spring 2008 Chapter 7 Stacks Read p 407-460. Problems 1-7.

2 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 3 Empty Stack Stack Operations

4 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 5 Stack (array implementation)

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

7 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 8 Empty Stack

9 9 initializeStack and destroyStack template void stackType ::initializeStack() { stackTop = 0; } template void stackType ::destroyStack() { stackTop = 0; }

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

11 11 Push

12 12 Push template void stackType ::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 13 Return Top Element template Type stackType ::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 14 Pop template void stackType ::pop() { if(!isEmptyStack()) stackTop--; //decrement stackTop else cerr<<"Cannot remove from an empty stack."<<endl; } Alternate pop: template Type stackType ::pop(Type& poppedElem) { if(!isEmptyStack()){ poppedElem = list[stackTop - 1]; stackTop--; } else cerr<<"Cannot remove from an empty stack."<<endl }

15 15 Pop

16 16 copyStack template void stackType ::copyStack(const stackType & 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 17 Copy Constructor template stackType ::stackType(const stackType & otherStack) { list = NULL; copyStack(otherStack); } Overloading the Assignment Operator (=) template const stackType & stackType ::operator= (const stackType & otherStack) { if(this != &otherStack) //avoid self-copy copyStack(otherStack); return *this; }

18 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 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 20 Is There Another Stack Implementation? Empty linked stack Nonempty linked stack Yes: linked (see linkedStack.h)

21 21 Push Stack before the push operation Stack and newNode

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

23 23 Pop Stack before the pop operation

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

25 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 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 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 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 29 Postfix Expression Calculator (Main Algorithm)

30 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 31 Convert a series of characters representing and integer, to the integer E.g. 12345 is 1*10 4 + 2 * 10 3 + 3 * 10 2 + 4 * 10 1 + 5 * 10 0 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?

32 32


Download ppt "1 CS 132 Spring 2008 Chapter 7 Stacks Read p 407-460. Problems 1-7."

Similar presentations


Ads by Google