Stack in Linked List in DSA using C++

Program 1

// Program for Stack Linked list
#include<iostream>
#include<stdio.h>
#define clrscr() system("cls")
using namespace std;
class node
{
    public:
      int data;
      node *add;
};
   node *start=NULL,*new1,*temp,*top,*prv;
   void create();
   void push();
   void pop();
   void display();
   int main()
   {
         clrscr();
         int choice;
       do  
        {
         cout<<"\n--------------------Stack Linked List---------------------------";
         cout<<"\n      1.Create ";
         cout<<"\n      2.Push ";
         cout<<"\n      3.Pop ";
         cout<<"\n      4.Display ";
         cout<<"\n      5.Exit ";
         cout<<"\n------------------------------------------------------------------";
         cout<<"\n Enter your choice: ";
         cin>>choice;
         switch(choice)
         {
            case 1: create();break;
            case 2: push();break;
            case 3: pop();break;
            case 4: display();break;
            case 5:break;
         }
        }while(choice!=5);
        return 0;
   }
   void create()
   {
        int n;
        char ch;
        cout<<"\nEnter an element";
        cin>>n;
        start=new node;
        start->data=n;
        start->add=NULL;
        temp=start;
        cout<<"\n Want tp continue";
         cin>>ch;
         while(ch=='y' || ch=='Y')
         {
            cout<<"\nEnter next element";
            cin>>n;
             new1=new node;
             new1->data=n;
             new1->add=NULL;
             temp->add=new1;
             temp=temp->add;
             cout<<"\n Want to continue";
            cin>>ch;
         }
   }
   void push()
   {
         int n;
       if(start==NULL)
         cout<<"\nStack List  not created please call create function first.....";
       else
       {
             cout<<"\n Enter an element for push";
             cin>>n;
             new1=new node;
             new1->data=n;
             new1->add=NULL;
             top=start;
             while(top->add!=NULL)
             {
                top=top->add;
             }
             top->add=new1;
       }

   }
   void pop()
   {
     if(start==NULL)
         cout<<"\nStack is empty";
       else
       {
             top=start;
             while(top->add!=NULL)
             {
                prv=top;
                top=top->add;
             }
             prv->add=NULL;
          cout<<"Poped element is: "<<top->data;
          delete top;
       }
   }
   void display()
   {
       if(start==NULL)
         cout<<"\nList not found";
       else
       {
             temp=start;
             while(temp!=NULL)
             {
                cout<<temp->data<<"   ";
                temp=temp->add;
             }
        
       }  
   }

 

courses
Image

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *