sahajjain01

Dynamic Stack (Menu Driven)

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