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

Slides:



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

C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
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
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
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.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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 CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
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
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.
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.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Chapter 7 Stacks Dr. Youssef Harrath
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.
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
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
CHP-3 STACKS.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
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.
Computer Science Department Data Structure and Algorithms Lecture 3 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.
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 4 Stacks
Data Structures Using C++ 2E
Stacks and Queues Chapter 4.
Stacks.
Data Structures Array Based Stacks.
Stacks Stack: restricted variant of list
Stacks Chapter 4.
Data Structures – Week #3
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 and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

1 CS 132 Spring 2008 Chapter 7 Stacks Read p 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 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 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 void stackType ::initializeStack() { stackTop = 0; } template void stackType ::destroyStack() { stackTop = 0; }

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

11 Push

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 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 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 Pop

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 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 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? Empty linked stack Nonempty linked stack Yes: linked (see linkedStack.h)

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 pop operation

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 * 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 * + 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)

* +: 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_|

* + 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) = = 11! How do we do it in a program? Assume input is followed by = : * + = 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 is 1* * * * * 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 ? What about ?

32