#include<conio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
class SStack
{
private:
    int *arr=NULL;
    int top=0;
public:
    SStack(int size)
    {
        arr=new int[size];
    }
    SStack()
    {
        arr=new int[5];
    }

    void push(int x)
    {
        if(top>5)
        {
            cout<<"stack is full";
            return;
        }
        arr[top++]=x;
         cout<<"successfully inserted:"<<x;
    }
    void pop()
    {
        if(top<0)
        {
            cout<<"stack is empty";
            return;
        }
        cout<<"the deleted item is:"<<arr[top--];
    }
    void display()
    {
        if(top<0)
        {
            cout<<"the stack is empty";
            return;
        }
        for(int i=top;i>=0;i--)
            cout<<arr[i]<<"  ";
    }
};
int main()
{
      int ch;
     SStack *stk=new SStack[10];
     SStack st;
     while(1)
     {
         cout<<"\n press 1 to push \n press 2 to pop  \n press 3 to display \n 4 to exit";
         cin>>ch;
         switch(ch)
         {
             case 1:cout<<"enter  the element";
             cin>>ch;
             st.push(ch);
             break;
             case 2:st.pop();
             break;
             case 3:st.display();
             break;
             case 4:exit(0);
         }

     }
     return(0);
}

Are you having an issue with your code are are you just showing us it? If you are having a problem explain what it is. If you are having compiler errors tell us that the error is and what line it is happening on.

it's code

#include <iostream>
#include <stdlib.h>
using namespace std;

class myStack
{
    private:
        int *pStack;
        int Index;
        int _Size;
    public:
        myStack(int Size);
        void push(int _v);
        void pop();
        void display();
};

myStack::myStack(int Size){

    pStack = new int[Size];
    Index = 0;
    _Size = Size;
}

void myStack::push(int _v){

    if( Index > (_Size-1) ){
        cout<<"stack is full";
        return;
    }

    pStack[Index++] = _v;
}

void myStack::pop(){

    if( Index == 0 ){
        cout << "stack is empty" << endl;
        return;
    }

    pStack[Index--] = 0;
}

void myStack::display(){

    if( Index == 0 ){
        cout << "the stack is empty" << endl;
        return;
    }
    cout << "<< Stack List => ";

    for ( int i=0;i<Index ;i++ )
    {
        cout << pStack[i] << " ";
    }

    cout << ">>";
}

int main(){

    int ch;
    myStack _stack(3);

    while(1){
        cout<<"\n press 1 to push \n press 2 to pop  \n press 3 to display \n press 4 to exit\n =>";
        cin>>ch;

        switch(ch){
            case 1:
                cout<<"enter  the element\n =>";
                cin>>ch;
                _stack.push(ch);
                break;
            case 2:
                _stack.pop();
                break;
            case 3:
                _stack.display();
                break;
            case 4:
                exit(0);
        }

    }

    return 0;
}

Edited 1 Year Ago by dbfud1988

This article has been dead for over six months. Start a new discussion instead.