



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An explanation of a stack data structure, its implementation using c++, and its application in the base-two conversion algorithm. The code for a stack class, its functions, and a test driver program. It also discusses the importance of stack invariants, pre- and post-conditions, and debugging.
Typology: Slides
1 / 43
This page cannot be seen from the preview
Don't miss anything!




































/* This algorithm displays the base-2 representation of a base-10 number. Receive: a positive integer number Output: the base-two representation of number. ---------------------------------------------------------------*/
a. Calculate the remainder that results when number is divided by 2. b. Push remainder onto the stack of remainders. c. Replace number by the integer quotient of number divided by 2.
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 << "\nMore (Y or N)? "; cin >> response; } while (response == 'Y' || response == 'y'); }
Define data members: consider storage structure(s) Attempt #1: Use an array with the top of the stack at position 0. e.g., Push 75, Push 89, Push 64, Pop
0 1 2 3 4
0 1 2 3 4
75
Push 75 0 1 2 3 4
89
Push 89 75
0 1 2 3 4
89
Push 64
75
64 0 1 2 3 4
89
Pop 75
Keep the bottom of stack at position 0. Maintain a "pointer" myTop to the top of the stack.
Instead of modeling a stack of plates, model a stack of books (or a discard pile in a card game.)
75 89
4 3 2 1 0
Push 75 Push 89
75
89
Push 64
75
64
Pop 4 3 2 1 0
4 3 2 1 0
4 3 2 1 0
4 3 2 1 0
89 75
64
myTop→
myTop→
myTop→ myTop→ myTop→
myTop = -1 myTop = 0 myTop = 1 myTop = 2 myTop = 1
Note: No moving of array elements.
Note: We don't clear this.
If we put typedef of StackElement and declaration of the constant STACK_CAPACITY in public section of the class declaration, they can be accessed outside the class, but require qualification: Stack::StackElement Stack::STACK_CAPACITY (for class) s.STACK_CAPACITY (for object)
#ifndef __STACK_H #define __STACK_H
const int STACK_CAPACITY = 128; typedef int StackElement;
class Stack { /***** Function Members *****/ public:
... /***** Data Members *****/ private: StackElement myArray[STACK_CAPACITY]; int myTop; }; // end of class declaration ... #endif
class Stack { public: /*--- Constructor --- Precondition: A stack has been declared. Postcondition: Stack has been constructed as an empty stack. */ Stack(); ... } // end of class declaration
inline Stack::Stack() { myTop = -1; }
NOTE THE DOCUMENTATION
Receives Stack containing it as a function member (perhaps implicitly) Returns: True if stack is empty, false otherwise. Member function? (^) Yes
Yes
Const function? (Shouldn't alter data members)? Yes Simple enough to inline? class Stack { public:
... /* --- Is the Stack empty? ---
inline bool Stack::empty() const { return (myTop == -1); }