Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Stacks Dr. Youssef Harrath

Similar presentations


Presentation on theme: "Chapter 7 Stacks Dr. Youssef Harrath"— Presentation transcript:

1 Chapter 7 Stacks Dr. Youssef Harrath yharrath@uob.edu.bh

2 Outline 1. Introduction 2. Implementation of Stacks as Arrays 3. Example: Highest GPA 4. Linked Implementation of Stacks 5. Stack as Derived from the class linkedListType 6. Application of Stacks: A. Postfix Expression Calculator B. Print a Linked List Backwards 2 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

3 1. Introduction 3 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Stack of coins Stack of books

4 1. Introduction 4 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011  A stack is a list of homogenous elements, wherein the addition and deletion of elements occurs only at one end (top of the stack).  The bottom element of the stack is the earliest added one.  The top element is the last added item.  The last added item will be the first one to be deleted.  A stack is called a LIFO data structure.

5 1. Introduction: Stack operations 5 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 A push box A A B push box B A B C push box C A B C Retrieve the top element A B pop stack

6 1. Introduction: Stack operations 6 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 The basic operations on a stack are:  initializeStack: intializes the stack to an empty state.  destroyStack: Removes all the elements from the stack, leaving the stack empty.  isEmptyStack: Checks whether the stack is empty.  isFullStack: Checks whether the stack is full.  push: Adds a new element to the top of the stack.  top: returns the top of the stack.  pop: Removes the top of the stack.

7 2. Implementation of Stacks as Arrays 7 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 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; // variable to point to the top of the stack Type *list; void copyStack(const stackType & otherStack); };

8 [99] [3] [2] [1] [0] 2. Implementation of Stacks as Arrays 8 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 A B C D...... 100 4 maxStackSize stackTop list stack

9 2. Implementation of Stacks as Arrays 9 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template void stackType ::initializeStack() { stackTop = 0; } template void stackType ::destroyStack() { stackTop = 0; }

10 2. Implementation of Stacks as Arrays 10 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template bool stackType ::isFullStack() { return(stackTop == maxStackSize); } template bool stackType ::isEmptyStack() { return(stackTop == 0); }

11 2. Implementation of Stacks as Arrays: push 11 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011  Store newItem in the array component indicated by stackTop.  Increment stackTop. S...... [99] [3] [2] [1] [0] 100 4 maxStackSize stackTop list stack u n n S...... [99] [4] [3] [2] [1] [0] 100 5 maxStackSize stackTop list stack u n n y push(‘y’)

12 2. Implementation of Stacks as Arrays: push 12 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template void stackType ::push(const Type& newItem) { if(!isFullStack()) { list[stackTop] = newItem; stackTop++; } else cout<<"Cannot add to a full stack."<<endl; }

13 2. Implementation of Stacks as Arrays: top 13 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template Type stackType ::top() { assert(stackTop != 0); return list[stackTop - 1]; }

14 2. Implementation of Stacks as Arrays: pop 14 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 B...... [99] [3] [2] [1] [0] 100 4 maxStackSize stackTop list stack o l d B...... [99] [3] [2] [1] [0] 100 3 maxStackSize stackTop list stack o l d pop()

15 2. Implementation of Stacks as Arrays: pop 15 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template void stackType ::pop() { if(!isEmptyStack()) stackTop--; else cout<<"Cannot remove from an empty stack."<<endl; }

16 2. Implementation of Stacks as Arrays: copyStack 16 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template void stackType ::copyStack(const stackType & otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; assert(list != NULL); for(int j = 0; j < stackTop; j++) list[j] = otherStack.list[j]; }

17 2. Implementation of Stacks as Arrays: constructor 17 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template stackType ::stackType(int stackSize) { if(stackSize <= 0) { cout<<"The size must be positive!"<<endl; maxStackSize = 100; } else maxStackSize = stackSize; stackTop = 0; list = new Type[maxStackSize]; assert(list != NULL); }

18 2. Implementation of Stacks as Arrays: destructor & copy constructor 18 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template stackType ::~stackType() { delete [] list; } template stackType ::stackType(const stackType & otherStack) { list = NULL; copyStack(otherStack); }

19 2. Implementation of Stacks as Arrays: overloading(=) 19 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template const stackType & stackType ::operator= (const stackType & otherStack) { if(this != &otherStack) copyStack(otherStack); return *this; }

20 3. Example: Highest GPA 20 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Write a C++ program that reads a data file consisting of each student’s GPA followed by the student’s name. The program then prints the highest GPA and the names of all the students who received that GPA. The program scans the input file only once. 3.9Marwa 3.1Ali 3.9Ahmad 2.1Salma 3.9Zahra 1.8Salah

21 3. Example: Highest GPA: Algorithm 21 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Read the 1 st GPA, the name of the 1 st student, and assign the 1 st GPA to the highestGPA. Read the 2 nd GPA, the name of the 2 nd student. We compare the second GPA with the highestGPA: 1.The new GPA > highestGPA a.Update the value of the highestGPA b.Destroy the stack c.Insert the name of the new student into the stack. 2.The new GPA = highestGPA: add the name to the stack 3.The new GPA < highestGPA: we do nothing Read the next GPA and student name and repeat the steps 1, 2, and 3 until we rich the end of the file.

22 3. Example: Highest GPA: Algorithm 22 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 1.Declare the variables 2.Open the input file (treat the exception case) 3.Read the GPA and the student name 4.highestGPA = GPA 5.Initialize the stack 6.while(not end of file) 1.if(GPA > highestGPA) 1.destroyStack(stack) 2.push(stack, student name) 3.highestGPA = GPA 2.else if (GPA = highestGPA) 3.Read the GPA and student name 7.Output the highest GPA 8.Output the names of the students having the highest GPA

23 3. Example: Highest GPA: Algorithm 23 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Program

24 4. Linked Implementation of Stacks: PartI 24 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template struct nodeType { Type info; nodeType link; } template class linkedStackType { public: const linkedStackType & operator=(const linkedStackType &); void initializeStack(); bool isEmptyStack(); bool isFullStack(); void destroyStack(); void push(const Type& newItem); Type top(); …

25 4. Linked Implementation of Stacks: Part II 25 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 … void pop(); linkedStackType(); linkedStackType(const linkedStackType & otherStack); ~ linkedStackType (); private: nodeType *stackTop; void copyStack(const stackType & otherStack); };

26 4. Linked Implementation of Stacks 26 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 stackTop stack Empty linked stack stackTop stack Nonempty linked stack C B A

27 4. Linked Implementation of Stacks: default constructor 27 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template linkedStackType ::linkedStackType() { stackTop = NULL; }

28 4. Linked Implementation of Stacks: destroyStack 28 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template void linkedStackType ::destroyStack() { nodeType *temp; while(stackTop != NULL) { temp = stackTop; stackTop = stackTop->link; delete temp; } }

29 4. Linked Implementation of Stacks: initializeStack 29 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template void linkedStackType ::initializeStack() { destroyStack(); }

30 4. Linked Implementation of Stacks: isEmptyStack & isFullStack 30 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template bool linkedStackType ::isEmptyStack() { return (stackTop == NULL); } template bool linkedStackType ::isFullStack() { return false; }

31 4. Linked Implementation of Stacks: push 31 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 stackTop stack C B A push(D) stackTop stack C B A D newNode

32 4. Linked Implementation of Stacks: push 32 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template void linkedStackType ::push(const Type& newElement) { nodeType *newNode; newNode = new nodeType ; assert(newNode != NULL); newNode->info = newElement; newNode->link = stackTop; stackTop = newNode; }

33 4. Linked Implementation of Stacks: top 33 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template Type linkedStackType ::top() { assert(newNode != NULL); return stackTop->info; }

34 4. Linked Implementation of Stacks: pop 34 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 stackTop stack C B A pop() stackTop stack C B A temp

35 4. Linked Implementation of Stacks: pop 35 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 template void linkedStackType ::pop() { nodeType *temp; if(stackTop != NULL) { temp = stackTop; stackTop = stackTop->link; delete temp; } else cout<<“cannot delete from an empty stack!”<<endl; }

36 5. Stack as Derived from the class linkedListType 36 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 pushinsertFirst initializeStack isEmptyStack initializeList isEmptyList The class linkedStackType can be derived from the class linkedListType

37 5. Stack as Derived from the class linkedListType 37 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Program

38 The operator is after/before the operands Left to right evaluation No precedence No need for () 6. Application of Stacks: Postfix Expression Calculator 38 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Infix notation a + ba b ++ a b Postfix notationPrefix notation The operator is between the operands Left to right evaluation Precedence () Lukasiewicz (Poland, 1950)

39 6. Application of Stacks: Postfix Expression Calculator 39 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Infix Expression a + b a + b * c a * b + c (a + b) * c (a - b) * (c + d) (a + b) * (c - d / e) + f a b – c d + * Equivalent Postfix Expression a b + a b c * + a b * c + a b + c * a b + c d e / - * f +

40 6. Application of Stacks: Postfix Expression Calculator 40 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Infix (6 + 3) * 2 = Postfix 6 3 + 2 * = 6 push(6) 6 pop() 9 push(9) 18 push(18) 3 6 push(3) 9 2 push(2)pop() 9

41 6. Application of Stacks: Postfix Expression Calculator 41 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 Program will be given as a lab assignment

42 6. Application of Stacks: Print a Linked List Backwards 42 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 1015520 first stackTop stack  10  15  5  20

43 6. Application of Stacks: Print a Linked List Backwards 43 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011 current = first; while(current != NULL) { stack.push(current); current = current->link; } Step 1: save the addresses of the nodes in a stack. while(!stack.isEmpty()) { current = stack.top(); stack.pop(); cout info<<“ “; } Step 2: print the linked list in reverse order.


Download ppt "Chapter 7 Stacks Dr. Youssef Harrath"

Similar presentations


Ads by Google