Stacks and Queues Implementation Code

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

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

View Full Document Right Arrow Icon
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; } //*************************************************
Background image of page 1

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

View Full DocumentRight Arrow Icon
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; }
Background image of page 2
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;
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online