Friday, November 25, 2011

19)a smalll prog To implement linked queue with options to add, delete, display and exit.




#include<iostream.h>
#include<conio.h>
#include<process.h>

struct node
{ int info;
  node* next;
};

void main()
{
 clrscr();
 node* front,*rear;
 char ch;
  do
   {
    front=rear=NULL;
     cout<<"\n";
     cout<<"1 to add\n";
     cout<<"2 to delete\n";
     cout<<"3 to display\n";
     cout<<"enter choice";
     cin>>ch;

  switch(ch)
   {
    case 1: rear=addQ(front,rear);
             if (front==NULL)
             front=rear;
             break;
    case 2: front=delQ(front,rear)
 if(front==NULL)
             rear=NULL;
             break;
    case 3: display();
              break;
    case 4:exit(0);
             break;
  }

}while(ch<4);
getch();
}

node*addQ(node*front,node*rear)
{
  node*ptr;
  ptr=new node;
  cout<<"enter info:";
  cin>>ptr----->info;
  ptr------>next=NULL;
   if(rear==null)
     front=rear=ptr;
   else
     { rear------>next=ptr;
        rear=ptr;
     }
 return rear;
}

node *delQ(node*front,node*rear)
{node*ptr;
 if(front==NULL)
   cout<<"empty!";
 else if(front==rear)
   { ptr=front;
     front=rear=NULL;
     delete ptr;
   }
   else
   {
    ptr=front;
    front=front----->next;
    delete ptr;
   }
return front;
}
void display()
{ node*ptr;
  if(front==NULL)
  cout<<"empty";
   else
     { ptr=front;
       while(ptr!=NULL)
         { cout<<ptr------->info;
           ptr=ptr-------->next;
         }
      }
}

/* OUTPUT

1 to add
2 to delete
3 to display
enter choice1
enter info13

1 to add
2 to delete
3 to display
enter choice3
13

1 to add
2 to delete
3 to display
enter choice2

1 to add
2 to delete
3 to display
enter choice3
empty

1 to add
2 to delete
3 to display
enter choice4
*/