Showing posts with label stack. Show all posts
Showing posts with label stack. Show all posts

Monday, 21 September 2015

Stack implementation using linked list

#include <iostream>
using namespace std;
struct node
   {
     int data ;
     node* next;
   };
node* head;
void insert(int x)
    {   node * temp=new node();
        temp->data=x;
          if(head==NULL)
             {
                 head =temp;
                 temp->next=NULL;
             }
  else
     {   temp->next=head;
        head=temp;
     }
}
void del()
{
     if(head==NULL)
        cout<<"Stack is empty"<<endl; 
    else
    head=head->next;
   
}
void show()
{ node*temp1=head;
while(temp1!=NULL)
{cout<<temp1->data<<"->";
       
       
    temp1=temp1->next;
    }
    cout<<"NULL"<<endl;
    }
int main()
{head=NULL;
char ch='y';int n;
    int x;
while(ch=='y'||ch=='Y')
    {
        cout<<"Enter 1 for insertion"<<endl;
        cout<<"Enter 2 for deletion"<<endl;
        cin>>n;
         switch (n)
        { case 1 : cout<<"Enter data:";
                   cin>>x;
                
           insert (x);
            break;
case 2 : del();
        break;
}
        cout<<"The the Stack is:"<<endl;
        show();
        cout<<"Do you want to continue operation  y/n"<<endl;
    cin>>ch;
    }
}