sahajjain01

Dynamic Queue (Menu Driven)

Feb 12th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. /*
  2. Each node of a Queue contains the following information, in addition to required pointer field:
  3. 1. Ticket No.
  4. 2. Passenger Name
  5. Write a menu driven program to Insert(), Display(), Delete() in a linked queue.
  6. */
  7. #include<iostream.h>
  8. #include<conio.h>
  9. #include<stdio.h>
  10. #include<stdlib.h>
  11. struct node
  12. {
  13.     int t_no;
  14.     char p_name[50];
  15.     node *link;
  16. };
  17.  
  18. node *temp=NULL;
  19. node *front=NULL;
  20. node *rear=NULL;
  21.  
  22. void insert()
  23. {
  24.     clrscr();
  25.     temp=new node;
  26.     cout<<"\n Enter ticket no.: ";
  27.     cin>>temp->t_no;
  28.     cout<<" Enter passenger name: ";
  29.     gets(temp->p_name);
  30.     temp->link=NULL;
  31.     if(front==NULL)
  32.     {
  33.         front=temp;
  34.         rear=temp;
  35.     }
  36.     else
  37.     {
  38.         rear->link=temp;
  39.         rear=temp;
  40.     }
  41.     cout<<"\n Insert Successful!";
  42.     cout<<"\n Press any key to continue...";
  43.     getch();
  44. }
  45. void display()
  46. {
  47.     clrscr();
  48.     if(front==NULL)
  49.     cout<<"\n The queue is empty!";
  50.     else
  51.  
  52.     {
  53.         temp=front;
  54.         while(temp!=NULL)
  55.         {
  56.             cout<<"\n Ticket no.: "<<temp->t_no;
  57.             cout<<"\n Passenger name: ";
  58.             puts(temp->p_name);
  59.             temp=temp->link;
  60.         }
  61.     }
  62.     cout<<"\n Press any key to continue...";
  63.     getch();
  64. }
  65. void delete_node()
  66. {
  67.     clrscr();
  68.     if(front==NULL)
  69.     cout<<"\n The queue is empty!";
  70.     else
  71.     {
  72.         temp=front;
  73.         front=front->link;
  74.         cout<<"\n Deleted the following record: ";
  75.         cout<<"\n Ticket no.: "<<temp->t_no;
  76.         cout<<"\n Passenger name: ";
  77.         puts(temp->p_name);
  78.         delete temp;
  79.     }
  80.     cout<<"\n Press any key to continue...";
  81.     getch();
  82. }
  83. void main()
  84. {
  85.     clrscr();
  86.     int choice;
  87.     while(1)
  88.     {
  89.         clrscr();
  90.         cout<<"\n\t\t\tMain Menu\n\t1.Insert\n\t2.Display\n\t3.Delete\n\t4.Exit";
  91.         cout<<"\n Enter your choice: ";
  92.         cin>>choice;
  93.         switch(choice)
  94.         {
  95.             case 1:
  96.                 insert();
  97.                 break;
  98.  
  99.             case 2:
  100.                 display();
  101.                 break;
  102.  
  103.             case 3:
  104.                 delete_node();
  105.                 break;
  106.  
  107.             case 4:
  108.                 exit(0);
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment