Menu driven program for Stack operations (Using Linked List)

 

Menu driven program for Stack operations (Using Linked List)

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 . The implementation of stack is done using Linked List.

Write a menu driven program for Stack operations (Using Linked List)

a) Creation of Stack

b) Traversing of Stack

c) Push Operation

d)Pop Operation

#include <bits/stdc++.h>

using namespace std;

struct Node

{

    int data;

    struct Node *next;

}*top=NULL;

 

void Push(int x)

{

    struct Node *t;

    t=new Node;

    if(t==NULL)

    {

        cout<<"Stack is full";

    }

    else

    {

    t->data=x;

    t->next=top;

    top=t;

    }

}

 

int Pop()

{

      struct Node *t;

     int x=-1;

    if(top==NULL)

    cout<<"Stack is empty";

    else

    {

     x=top->data;

     t=top;

      top=top->next;

    delete t;

    }

     return x;

}

 

void display()

{

    struct Node *t;

    t=top;

    while(t)

    {

        cout<<t->data<<" ";

        t=t->next;

    }

}

int main()

{

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

    do

    {

        cout<<"1. Push in Stack"<<endl<<"2. Pop from Stack "<<endl<<"3. Display"<<endl<<"4. Exit"<<endl;;

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

        cin>>option;

        switch(option)

        {

       

        case 1:

        {

            cout<<"Enter element : ";

            cin>>x;

            Push(x);

            cout<<endl;

            break;

        }

        case 2:

        {

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

            cout<<endl;

            break;

        }

        case 3:

        {

            cout<<"Displaying elements :";

            display();

            cout<<endl;

            break;

        }

        default:

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

        }

    }while(option<=3);

    return 0;}

Inserting elements in Stack

Popping and Displaying elements from Stack


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 )