Make Your Search Easy ! :) Use me

Friday, February 17, 2017

Dynamic Queue / Linked List

Insert and Delete Operations in a Linked List / Dynamic Queue :-

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

struct NODE
 {
    int TrainNo;
      char TrainName[20];
      NODE *Link;
   };

NODE *top=NULL,*end=NULL;

void insert()
 {
    NODE *p;
    if(!(malloc(sizeof(p))))
       {
          cout<<"\nMEMORY EXHAUSTED";
            return;
         }
      p=new NODE;
      cout<<"Enter Train Number : ";
      cin>>p->TrainNo;
      cout<<"Enter Train Name : ";
      gets(p->TrainName);
      if(end==NULL)
       {
          top=end=p;
         }
      end->Link=p;
      end=p;
      end->Link=NULL;
      cout<<"\nElement Inserted";
   }

void remove()
 {
      NODE *p;
  if(top==NULL)
       {
          cout<<"\nUNDERFLOW";
            return;
         }
      p=top;
      top=top->Link;
      cout<<"Removed Elemnent is"<<p->TrainNo<<endl;
      delete p;
   }

void show()
 {
    NODE *p;
      if(top==NULL)
       {
          cout<<"\nQueue is empty";
            return;
         }
      p=top;
      while(p!=NULL)
       {
            cout<<"\n"<<p->TrainNo<<"\t"<<p->TrainName;
            p=p->Link;
         }
   }

void main()
 {
      int c;
      while(1)
       {
            cout<<"\n\nPress 1 - to insert element\nPress 2 - to delete element\nPress 4 -  to exit:\n";
            cin>>c;
            switch(c)
             {
                case 1:
                   insert();
                     show();
                     break;
                  case 2:
                   remove();
                     show();
                     break;
                  case 4:
                   exit(0);
                  default:
                   cout<<"INVALID OPTION\nChoose valid option from menu.....\n";
                     break;
               }
         }
   }

No comments:

Post a Comment