Chapter 7 Stacks Dr. Youssef Harrath

Slides:



Advertisements
Similar presentations
TK1924 Program Design & Problem Solving Session 2011/2012
Advertisements

DATA STRUCTURES USING C++ Chapter 5
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.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Data Structures & Algorithms
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Linked Lists Dr. Youssef Harrath
Chapter 18: Stacks and Queues
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Chapter 18: Stacks and Queues
Data Structures Using C++ 2E
Chapter 17: Stacks and Queues
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Chapter 18: Stacks and Queues
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
1 Chapter 17: Stacks and Queues Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a.
Chapter 3 Pointers and Array-Based Lists Dr. Youssef Harrath
Chapter 11 Binary Trees Dr. Youssef Harrath
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Chapter 16: Linked Lists.
Chapter 4 Stacks
Data Structures Using C++ 2E
C++ Programming:. Program Design Including
Stacks and Queues Chapter 4.
Homework 4 questions???.
C++ Plus Data Structures
Data Structures Array Based Stacks.
Stacks Stack: restricted variant of list
Algorithms and Data Structures
Stack.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Data Structures Array Based Stacks.
Stacks Data structure Elements added, removed from one end only
Stacks CS-240 Dick Steflik.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack.
Presentation transcript:

Chapter 7 Stacks Dr. Youssef Harrath

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

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

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.

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

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.

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); };

[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 maxStackSize stackTop list stack

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; }

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); }

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] maxStackSize stackTop list stack u n n S [99] [4] [3] [2] [1] [0] maxStackSize stackTop list stack u n n y push(‘y’)

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; }

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]; }

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

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; }

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]; }

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); }

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); }

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; }

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

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.

3. Example: Highest GPA: Algorithm 22 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/ 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

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

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(); …

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); };

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

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

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; } }

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

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; }

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

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; }

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; }

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

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; }

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

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

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)

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 +

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

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

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

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.