Tuesday 14 May 2013

C++ Stack - Part 2


The stack's function definitions and implementations. To view the stack interface go to part 1.


#include<iostream>
#include "part1_filename.h"    

using namespace std;

stack::stack()                 
{
     top = -1;
}

stack::~stack(){ }

bool stack::Push(int x)
{

    if(IsFull())
    {
        cout <<"The stack is full!"<<endl;
        return 0;
    }
    arrayStack[++top] = x;
}

bool stack::Pop()
{
    if( IsEmpty() )
    {
        cout <<"The stack is empty";
        cout<<endl;
        return 0;
    }
    else
    cout<<arrayStack[top--]<<"  has been removed from the stack"<<endl;
}

bool stack::IsEmpty()
{    
    return (top == -1);
     
}

bool stack::IsFull()
{
   return (top > 8);  
}

void stack::Print()
{
    for(int z=top; z>=0; z--){
    cout <<arrayStack[z] <<"    ";
    }
    cout<<endl;
}

int stack::getSize()
{
    top = top + 1;
    cout << top;
}

Save this block of code in the same folder as the header file in part one. Part three will cover the main function.

No comments:

Post a Comment