Thursday 13 October 2011

this is the demonstration of stack

#include <iostream>
using namespace std;

class base {

    int* stack;
    int size;
    int top;

public:
    void init(int _size);
    ~base();
    bool add(int data);
    int remove();
    bool isempty();
};



void base::init(int _size){
    size=_size;
    stack = new int [size];
    top = -1;
}



bool base::add(int data){
    if(top < size-1){
        top ++;
        stack[top]= data;
        return true;
    } else {
        return false;
    }
}

int base::remove(){

    if(top >= 0) {
        return stack[top--];
    } else {
        return -1;
    }
}

bool base::isempty(){
    if (top == -1 ){
        cout << "\nNow stack is empty" << endl;
        return true;

    }else{
        return false;
    }
}

base::~base(){
    delete stack;
}

void main(){

    int cpush;
    int vpush;
    int i = 0;
    base b;

    b.init(256);
    cout << "How many data you want to add ? :";
    cin >> cpush;

    for(int i=0 ; i < cpush ; i++){
      
        cout << "\nInput data (numbers) : ";
        cin >> vpush;
        b.add(vpush);
        }
  
    while(!b.isempty()){

        cout << "\n" << i + 1 << "th pop : "<<b.remove() << endl;
        i++;
    }

  
    //cout << b.remove() << endl;
    //cout << b.remove() << endl;
    //cout << b.remove() << endl;

}

No comments:

Post a Comment