Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 Stacks.

Similar presentations


Presentation on theme: "Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 Stacks."— Presentation transcript:

1 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 Stacks Chapter 7

2 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 2 Chapter Contents 7.1 Introduction to Stacks 7.2 Designing and Building a Stack Class – Array-Based 7.3 Linked Stacks 7.4 Use of Stacks in Function Calls 7.5 Case Study: Postfix (RPN) Notation

3 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 3 Chapter Objectives Study a stack as an ADT Build a static-array-based implementation of stacks Build a linked-implementation of stacks Show how a run-time stack is used to store information during function calls Study postfix notation and see how stacks are used to convert expressions from infix to postfix and how to evaluate postfix expressions

4 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 4 Introduction to Stacks Consider a card game with a discard pile –Discards always placed on the top of the pile –Players may retrieve a card only from the top We seek a way to represent and manipulate this in a computer program This is a stack What other examples can you think of that are modeled by a stack?

5 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 5 Introduction to Stacks A stack is a last-in-first-out (LIFO) data structure Adding an item –Referred to as pushing it onto the stack Removing an item –Referred to as popping it from the stack

6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 6 A Stack Definition: –An ordered collection of data items –Can be accessed at only one end (the top) Operations: –construct a stack (usually empty) –check if it is empty –Push: add an element to the top –Top: retrieve the top element –Pop:remove the top element

7 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 7 Example Program Consider a program to do base conversion of a number (ten to two) Note program which assumes existence of a Stack class to accomplish this, Fig 7.2Fig 7.2 –Demonstrates push, pop, and top

8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 8 #include using namespace std; #include "Stack.h" // our own -- for STL version int main() { unsigned number, // the number to be converted remainder; // remainder when number is divided by 2 Stack stackOfRemainders; // stack of remainders char response; // user response do { cout << "Enter positive integer to convert: "; cin >> number; while (number != 0) { remainder = number % 2; stackOfRemainders.push(remainder); number /= 2; } cout << "Base-two representation: "; while ( !stackOfRemainders.empty() ) { remainder = stackOfRemainders.top(); stackOfRemainders.pop(); cout << remainder; } cout << endl; cout << "\nMore (Y or N)? "; cin >> response; } while (response == 'Y' || response == 'y'); }

9 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 9 Selecting Storage Structure Model with an array –Let position 0 be top of stack Problem … consider pushing and popping –Requires much shifting

10 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 10 Selecting Storage Structure A better approach is to let position 0 be the bottom of the stack Thus our design will include –An array to hold the stack elements –An integer to indicate the top of the stack Note beginning of Stack.h file, Fig. 7.3 Fig. 7.3 Note beginning of Stack.h file, Fig. 7.3 Fig. 7.3

11 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 11 Implementing Operations Constructor –Compiler will handle allocation of memory Empty –Check if value of myTop == -1 Push (if myArray not full) –Increment myTop by 1 –Store value in myArray [myTop] Top –If stack not empty, return myArray[myTop] Pop –If array not empty, decrement myTop Output routine added for testing

12 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 12 The Stack Class The completed Stack.h file, Fig. 7.4AFig. 7.4A –All functions defined –Note use of typedef mechanism Implementation file, Stack.cpp, Fig 7.4BFig 7.4B Driver program to test the class, Fig 7.5Fig 7.5 –Creates stack of 4 elements –Demonstrates error checking for stack full, empty

13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 13 #include #ifndef STACK #define STACK const int STACK_CAPACITY = 128; typedef int StackElement; class Stack { public: /***** Function Members *****/ Stack(); bool empty() const; void push(const StackElement & value); void display(ostream & out) const; StackElement top() const; void pop(); private: /***** Data Members *****/ StackElement myArray[STACK_CAPACITY]; int myTop; }; // end of class declaration #endif

14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 14 //--- Definition of Stack constructor Stack::Stack() : myTop(-1) { } //--- Definition of empty() bool Stack::empty() const { return ( ? ); }

15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 15 //--- Definition of push() void Stack::push(const StackElement & value) { if (myTop < STACK_CAPACITY - 1) { ? } else { cerr << "*** Stack full -- can't add new value ***\n" "Must increase value of STACK_CAPACITY in Stack.h\n"; exit(1); }

16 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 16 //--- Definition of top() StackElement Stack::top() const { if ( !empty() ) return ( ? ); else { cerr << "*** Stack is empty " " -- returning garbage value ***\n"; return *(new StackElement); }

17 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 17 //--- Definition of pop() void Stack::pop() { if ( !empty() ) { ? } else cerr << "*** Stack is empty -- " "can't remove a value ***\n"; }

18 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 18 Application of Stacks Consider events when a function begins execution Activation record (or stack frame) is created Stores the current environment for that function. Contents:

19 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 19 Run-time Stack Functions may call other functions –interrupt their own execution Must store the activation records to be recovered –system then reset when first function resumes execution This algorithm must have LIFO behavior Structure used is the run-time stack

20 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 20 // --- # includes and prototypes of functions here int main() { int a = 3; f1(a); cout << endl; // A: Return here after f1 finishes } // --- Function f1 void f1(int x) { cout << f2(x+1); // B: Return here after f2 finishes} // --- Function f2 void f2(int p) { int q =f3(p/2) ; // C: Return here after f3 finishes return 2*q;} // --- Function f3 int f3(int n) { return n*n + 1; }

21 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 21 Use of Run-time Stack When a function is called … Copy of activation record pushed onto run- time stack Arguments copied into parameter spaces Control transferred to starting address of body of function

22 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 22 Use of Run-time Stack When function terminates Run-time stack popped –Removes activation record of terminated function –exposes activation record of previously executing function Activation record used to restore environment of interrupted function Interrupted function resumes execution


Download ppt "Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 Stacks."

Similar presentations


Ads by Google