Advertisement
Guest User

Railway reservation

a guest
Dec 12th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>
  4. #define size 5
  5.  
  6.  
  7. typedef struct NODE
  8. {
  9.     int reg_no;
  10.     int age;
  11.     char name[20];
  12.     struct NODE *next;
  13. } node;
  14.  
  15. node* deq();
  16. void create();
  17. int reserve(node*);
  18. int cancel(int);
  19. void enq(node*);
  20. void display();
  21.  
  22.  
  23. node *start;
  24. node *front;
  25. node *rear;
  26. int count=0;
  27. int num=0;
  28.  
  29. void create( )
  30. {
  31.     node *new;
  32.     new=(node *)malloc(sizeof(node));
  33.     new->reg_no=1;
  34.     printf("Name: ");
  35.     scanf("%s", new->name);
  36.     printf("Age : ");
  37.     scanf("%d", &new->age);
  38.     start=new;
  39.     new->next=NULL;
  40.     num++;
  41.  
  42. }
  43.  
  44. int reserve(node *start)
  45. {
  46.  
  47.     if(start==NULL)
  48.     {
  49.          create(start);
  50.          return 1;
  51.     }
  52.     else
  53.     {
  54.  
  55.     node *temp, *new_node;
  56.     temp=start;
  57.     while(temp->next!=NULL)
  58.     {
  59.       temp=temp->next;
  60.     }
  61.  
  62.     new_node=(node *)malloc(sizeof(node));
  63.  
  64.     printf("Name: ");
  65.     scanf("%s", new_node->name);
  66.     printf("Age : ");
  67.     scanf("%d", &new_node->age);
  68.     new_node->next=NULL;
  69.     if(num<=size)
  70.     {
  71.         num++;
  72.         new_node->reg_no=num;
  73.         temp->next=new_node;
  74.  
  75.         return 1;
  76.     }
  77.     else
  78.     {
  79.         enq(new_node);
  80.         return 0;
  81.     }
  82. }
  83. }
  84.  
  85.  
  86. int cancel(int reg)
  87. {
  88.     node *ptr, *preptr, *new;
  89.     ptr=start;
  90.     preptr=NULL;
  91.     if(start==NULL)
  92.     return -1;
  93.     if(ptr->next==NULL && ptr->reg_no==reg)
  94.         {
  95.         start=NULL;
  96.         num--;
  97.         free(ptr);
  98.         return 1;
  99.  
  100.         }
  101.  
  102.     else{
  103.     while(ptr->reg_no!=reg && ptr->next!=NULL)
  104.         {
  105.             preptr=ptr;
  106.             ptr=ptr->next;
  107.         }
  108.         if(ptr==NULL && ptr->reg_no!=reg)
  109.             return -1;
  110.         else
  111.             preptr->next=ptr->next;
  112.         free(ptr);
  113.         new=deq();
  114.         while(preptr->next!=NULL)
  115.             preptr=preptr->next;
  116.         preptr->next=new;
  117.         num--;
  118.         return 1;
  119.  
  120.     }
  121. }
  122.  
  123. void enq(node *new_node)
  124. {
  125.     if(rear==NULL)
  126.     {
  127.         rear=new_node;
  128.         rear->next=NULL;
  129.         front=rear;
  130.     }
  131.     else
  132.     {
  133.         node *temp;
  134.         temp=new_node;
  135.         rear->next=temp;
  136.         temp->next=NULL;
  137.         rear=temp;
  138.     }
  139.     count++;
  140. }
  141.  
  142. node* deq()
  143. {
  144.     node *front1;
  145.     front1=front;
  146.     if(front==NULL)
  147.         return NULL;
  148.     else
  149.     {
  150.         count-- ;
  151.         if(front->next!=NULL)
  152.         {
  153.             front=front->next;
  154.             front1->next=NULL;
  155.             return front1;
  156.         }
  157.         else
  158.         {
  159.             front=NULL;
  160.             rear=NULL;
  161.  
  162.             return front1;
  163.         }
  164.     }
  165.  
  166.  
  167. }
  168.  
  169.  
  170. void display()
  171. {
  172.     node *temp;
  173.     temp=start;
  174.     while(temp!=NULL)
  175.     {
  176.         printf("\nRegistration Number: %d\n", temp->reg_no);
  177.         printf("Name : %s\n\n", temp->name);
  178.         temp=temp->next;
  179.     }
  180.  
  181. }
  182.  
  183. int main()
  184. {
  185.     int choice, status=0,canc=0, reg=0;
  186.     start=NULL;
  187.     rear=NULL;
  188.     front=NULL;
  189.  
  190.  
  191.  
  192.     printf("\t\t\t***RAILWAY RESERVATION***\t\t\t\t\n");
  193.     int ch =0;
  194.     while(ch!=4)
  195.     {
  196.     printf("\n\nDo you want to - \n 1. Reserve a ticket? \n 2. Cancel Booking \n 3. Display passenger details \n 4. exit\n\n");
  197.     scanf("%d", &choice);
  198.     switch(choice)
  199.     {
  200.         case 1 :  status=reserve(start);
  201.                   if(status==0)
  202.                     printf("\nBooking Full!! \nYou are added to waiting list. Waiting list number %d", count);
  203.                   else
  204.                     printf(" \nBooking Successful!!! Enjoy your journey! Your Reg No is %d\n\n", num);
  205.  
  206.                   break;
  207.  
  208.         case 2:   printf(" \n Give the Registration number to be cancelled\n");
  209.                   scanf(" %d", &reg);
  210.                   if(reg>num)
  211.                   printf("Invalid!!");
  212.                   else
  213.                   {
  214.                   canc=cancel(reg);
  215.                   if(canc==-1)
  216.                     printf("\nRegistration number invalid!!\n");
  217.                   else
  218.                     printf("\nRegistration cancelled successfully\n");
  219.                     }
  220.                   break;
  221.  
  222.         case 3: display();
  223.         break;
  224.         case 4: exit(0);
  225.         break;
  226.         default: printf("\nWrong choice!\n");
  227.  
  228.  
  229.  
  230.  
  231.     }
  232.  
  233. }
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement