Stacks and Queues Implementation Code

# Stacks and Queues Implementation Code - Stacks and Queues...

This preview shows pages 1–5. Sign up to view the full content.

1 Stacks and Queues CpE 360 – Computational Algorithms and Data Structures Instructor: Ashish Patel Reference: Starting out with C++ early objects, by Tony Gaddis, Addison Wesley, 5 th Edition, ISBN: 0-321-38348-6 Static Stacks Implementation (using Arrays) #ifndef INTSTACK_H #define INTSTACK_H class IntStack { private : int *stackArray; int stackSize; int top; public : IntStack( int ); ~IntStack() { delete [] stackArray;} void push( int ); void pop( int &); bool isFull(); bool isEmpty(); }; #endif #include <iostream> #include "intstack.h" using namespace std; //******************* // Constructor * //******************* IntStack::IntStack( int size) { stackArray = new int [size]; stackSize = size; top = -1; } //*************************************************

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 // Member function push pushes the argument onto * // the stack. * //************************************************* void IntStack::push( int num) { if (isFull()) { cout << "The stack is full.\n"; exit(1); } else { top++; stackArray[top] = num; } } //**************************************************** // Member function pop pops the value at the top * // of the stack off, and copies it into the variable * // passed as an argument. * //**************************************************** void IntStack::pop( int &num) { if (isEmpty()) { cout << "The stack is empty.\n"; exit(1); } else { num = stackArray[top]; top--; } } //*************************************************** // Member function isFull returns true if the stack * // is full, or false otherwise. * //*************************************************** bool IntStack::isFull() { bool status; if (top == stackSize - 1) status = true ; else status = false ; return status; }
3 //**************************************************** // Member function isEmpty returns true if the stack * // is empty, or false otherwise. * //**************************************************** bool IntStack::isEmpty() { bool status; if (top == -1) status = true ; else status = false ; return status; } // PROGRAM 1 // This program demonstrates the IntStack class. #include <iostream> #include "IntStack.h" using namespace std; int main() { IntStack stack(5); int catchVar; cout << "Pushing 5\n"; stack.push(5); cout << "Pushing 10\n"; stack.push(10); cout << "Pushing 15\n"; stack.push(15); cout << "Pushing 20\n"; stack.push(20); cout << "Pushing 25\n"; stack.push(25); cout << "Popping. ..\n"; stack.pop(catchVar); cout << catchVar << endl; stack.pop(catchVar); cout << catchVar << endl; stack.pop(catchVar); cout << catchVar << endl; stack.pop(catchVar); cout << catchVar << endl;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 stack.pop(catchVar); cout << catchVar << endl; return 0; } #ifndef MATHSTACK_H #define MATHSTACK_H #include "IntStack.h" class MathStack : public IntStack { public : MathStack( int s) : IntStack(s) {} void add(); void sub(); }; #endif #include "MathStack.h" //*************************************************** // Member function add. add pops
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 15

Stacks and Queues Implementation Code - Stacks and Queues...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online