Tuesday 23 October 2018

Implementation of "STACKS" using arrrays (Data structure)


#include <iostream>
#include <cstdlib>
using namespace std;

int stackarr[5];
int size=5;
int top=-1;
/***************************************************/
void push(int item)
{
    if(top==size-1)
    {
        cout<<"\n Stack is Overflow:"<<endl;
    }
    else
    {
        top++;
        stackarr[top]=item;
       cout<<item<<" \n Item is insorted"<<endl;
    }
}
/********************************************************/
int isFull()
{
    if(top=size-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
/*************************************************************/
int pop()
{
    if(top==-1)
    {
        return 0;
    }
    else
    {
        return stackarr[top--];
    }
}
/***************************************************************/
void disply()
{
    if(top==-1)
    {
        cout<<"\n Stack is Empty"<<endl;
    }
    else
    {
        for(int i=0;i<size;i++)
        {
            cout<<stackarr[i]<<" ";
        }
    }
}

/***************************************************************/
int isEmpty()
{
    if(top==-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }

}
/*************************************************************/
void topfun()
{
    if(isEmpty())
    {
        cout<<" \n Stack is Empty"<<endl;
    }
    else
    {
        cout<<"\n Top element= "<<stackarr[top]<<endl;
    }
}

/*************************************************************/
int main()
{
    int choice,item,r;
   while(1)
   {
    cout << "Enter 1 for push: " << endl;
    cout << "Enter 2 for pop: " << endl;
    cout << "Enter 3 for Top: " << endl;
    cout << "Enter 4 for display: " << endl;
    cout << "Enter 5 for exit: " << endl;
    cout<<"Enter your choice:"<<endl;
    cin>>choice;
        switch(choice)
        {
            case 1: cout<<"\n Enter items in stack: "<<endl;
                    cin>>item;
                    push(item);
                    break;

            case 2:     r=pop();
                        if(r==0)
                        {
                            cout<<"\n Stack is underflow"<<endl;
                        }
                        else
                        {
                            cout<<"\n ITem is popped "<<endl;
                        }
                        break;

            case 3: topfun();
            break;

            case 4: disply();
            break;

           case 5: exit(0);;
               break;

            default :
                cout<<"\n invalid input"<<endl;
        }
   }

    return 0;
}

0 comments:

Post a Comment