Stack Using Linked List

#include <iostream>
#include <conio.h>
#define MAX 10
using namespace std;
struct stack
    {
     int data;
     stack *prev, *next;
    }*ctr, *temp, *start=NULL;

void push();
void show();
void pop();

int  main()
{ int count=0;
   
    while(1)
    {
   
                cout <<"MAIN MENU\n" ;
                cout <<"\tPress 1 for PUSH \n" ;
                cout <<"\tPress 2 for SHOW \n" ;
                cout <<"\tPress 3 for POP \n" ;
                cout <<"\tPress 0 for exit\n\nAns. " ;
                char ch;
                cin>>ch;
                switch(ch)
                {
                     case '1':
                      count++;
                              if(count<=MAX)
                                                push();
                                           else
                                           cout<<"Stack if full";
       cout<<endl<<endl<<endl;
                                                break;
                     case '2':
                                               
                                                show();
                                                getch();
                                                break;
                     case '3':
                                           count--;
  if(count<=0)
  cout<<"Stack is Empty\n";
  else    
                                                pop();
                                                break;

                     case '0':
                   
                                            return 0;

                }
    }
 
getch();
return 0;
}
 void push()
 {
    temp = new stack;
    temp -> next = NULL;
    cout << "Enter data :  " ;
    cin >> temp -> data ;
    if(start == NULL)
    {
                start = temp;
                temp -> prev = NULL;
    }
    else
    {
                ctr= start;
                while(ctr->next != NULL)
                {
                    ctr = ctr->next;
                }
                ctr->next = temp;
                temp->prev = ctr;
    }

 }
 void show()
 { cout<<"\n\nStack\n\n";
       ctr=start;
       while(ctr->next!=NULL)
       {
                   ctr = ctr->next;
       }
       while(ctr!=NULL)
       {
        cout<<"-------"<<endl<<"|| "<<ctr->data<<" ||"<<endl<<"-------"<<endl;
        ctr=ctr->prev;
       }
 cout<<endl<<endl<<endl;
 }
 void pop()
 {
    temp=start;
    cout<<"\nDATA POPED \n";
    if(temp==NULL)
           cout<<"There is no Info present in Stack. Stack is empty.";
           else
           {
     while(temp->next!=NULL)
    {
     ctr=temp;
     temp=temp->next;
    }
     ctr->next=NULL;
   }
    cout<<endl<<endl<<endl;
 }

Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons