Stack

 #include <iostream>

#include <cstddef>

#include <climits>

using namespace std;


class stack{

    private:

    int*data;

    int nextindex;

    int capacity;

    public:

    stack(int TotalSize){

        data=new int[TotalSize];

        nextindex=0;

        capacity=TotalSize;

    }

    int size(){

        return nextindex;

    }

    bool isempty(){

        return nextindex==0;

    }

    void push(int element){

        if(nextindex==capacity){

            cout<<"Stack is Full";

            return;

        }

        data[nextindex]=element;

        nextindex++;

    }

    int pop(){

        if(nextindex==0){

            cout<<"Stack is Empty";

            return -1;

        }

        nextindex--;

        return data[nextindex];

    }

    int top(){

        if(nextindex==0){

            cout<<"Stack is Empty";

            return -1;

        }

       return data[nextindex-1];

    }

    

};


int main() {

    stack s(4);

    s.push(10);

    s.push(20);

    s.push(30);

    s.push(40);

    s.push(50);

   

    cout<<s.top()<<" "<<s.pop()<<" "<<s.pop()<<" "<<s.pop()<<" "<<s.pop()<<" ";

    cout<<s.isempty()<<" "<<s.size();

    return 0;

}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings