Menu driven program for Stack operations (Using Array)

 Menu driven program for Stack operations (Using Array)

Stack is a data structure that works on the principle of Last in First Out i.e. LIFO. The program given below is a C++ menu driven program for the implementation of stack and various operations used in stack like creation, traversing , push and pop operations . 

Menu driven program for Stack operations (Using Array)

a) Creation of Stack

b) Traversing of Stack

c) Push Operation

d)Pop Operation

#include <bits/stdc++.h>

 

using namespace std;

 

struct Stack

{

   int size;

   int top;

   int *s;

};

 

void create(struct Stack *st)

{

    cout<<"Enter size :";

    cin>>st->size;

    st->top=-1;

    st->s=new int [st->size];

}

 

void Push(struct Stack *st,int x)

{

    if(st->top==st->size-1)

        cout<<"Stack Overflow"<<endl;

      

    else

    {

    st->top++;

    st->s[st->top]=x;

    }

}

 

int Pop(struct Stack *st)

{

     int x=-1;

    if(st->top==-1)

    cout<<"Stack Underflow"<<endl;

   

    else

    {

    x=st->s[st->top];

    st->top--;

    }

    return x;

}

void display(struct Stack st)

{

    int i;

    for(i=st.top;i>=0;i--)

    {

        cout<<st.s[i]<<" ";

    }

    cout<<endl;

}

int main()

{

  struct Stack st;

  int option,n,pos,x,index,t;

    do

    {

        cout<<"1. Create stack"<<endl<<"2. Insert/Push in stack"<<endl<<"3. Delete/Pop "<<endl<<"4. Display"<<endl<<"5. Exit"<<endl;

        cout<<"Enter an option"<<endl;

        cin>>option;

        switch(option)

        {

        case 1 :

        {

           create(&st);

            break;

        }

        case 2:

        {

            cout<<"Enter element : "<<endl;

            cin>>x;

            Push(&st,x);

            cout<<endl;

            break;

        }

        case 3:

        {

            cout<<"Popped element is: "<<Pop(&st);

            cout<<endl;

            break;

        }

        case 4:

        {

            cout<<"Displaying elements :";

            display(st);

            cout<<endl;

            break;

        }

        default:

        cout<<"Exiting program......"<<endl;

        }

    }while(option<=4);

   

    return 0;

}

Creation of Stack using array

Insertion and Display in Stack using Array

Pop or Delete in Stack using Array


Comments

Popular posts from this blog

Notice Writing Format with Examples ( Notice writing )

CSAB (Fee Refund)

All Derivations of Ray Optics Class 12 ( Ray Optics )

Solid state previous year questions CBSE with answers( One mark)

JEE Mains -Mole Concept and Stoichiometry Previous Year Questions (with Answers)

CSAB 2020 ( CSAB Fee Refund )

Message Writing,Format for message writing

Electrostatics Important Five mark questions with answers for CBSE Class 12 (previous year)

Menu Driven Program For Circular Linked List: creation, traversing, insertion and deletion(Circular Linked List)

Solid state previous year questions CBSE with answers( Two and Three Mark )