Reservation of train using recursion

 Problem statement
Write a c RECURSIVE program to maintain reservation list of a train using link list,your program should do the following it should take the name,age,sex as input of a passenger, there are 50 seats in the train,and you can take up to 20 waiting list.
[Click here for non recursive]


#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
  int seat;
  char name[15];
  int age;
  char sex;
  struct node* next;
}list,*ptr;
void delete(ptr a);
void add(ptr a,int s_number);
void printseat(ptr temp);
void delete(ptr a);
int main()
{
  int i,input;
  ptr a;
  a=(ptr)malloc(1*sizeof(list));             // dummy node
  a->next=NULL;
  a->seat=0;
  for(i=0;i<100;i++)
    {
      printf("enter 1 to book a ticket and 2 for deleting a TICKETand 4 to exit the loop \n");
      scanf("%d",&input);
      if(input==1)
    {
      add(a,0);
    }
      else if(input==2)
    {
      delete(a);
    }
      else if(input==4)
    {
      break;
    }
      else
    {
      printf("not able to recognise the command \n");
    }
     
    }
  printf("thank you \n");
  return 0;
}
void add(ptr a,int s_number)
{

  ptr temp=NULL;
  if(a->next!=NULL)
    {
      return add(a->next,(a->seat)+1);
    }
  else if(s_number>50)
    {
      printf("%d waiting awailable \n",(s_number)-50);
    
    }
  temp=(ptr)malloc(1*sizeof(list));
  temp->next=NULL;
  printf("enter name ");
  scanf("%s",temp->name);
  printf("enter age ");
  scanf("%d",&temp->age);
  printf("enter sex ");
  scanf("%s",&temp->sex);
  temp->seat=(s_number)+1;
  printseat(temp);
  a->next=temp;
  return ;
}
void printseat(ptr temp)
{
  if(temp->seat>50)
    {
      printf("WL=%d",(temp->seat)-50);
    }
  else
    {
      printf("confirm seat=%d",temp->seat);
    }
  printf(" name=%s ",temp->name);
  printf("sex=%c ",temp->sex);
  printf("age=%d \n",temp->age);
  return ;
}

void delete(ptr a)
{
  int input;
  ptr temp;
  a=a->next;
  printf("enter the seat number ");
  scanf("%d",&input);
  while(a!=NULL&&(a->seat)!=input)
    {
      a=a->next;
    }
  if(a==NULL)
    {
      printf("can not delete \n");
      return ;   
    }
  temp=a;
  a=a->next;
  free(temp);
  while(a!=NULL)
    {
      a->seat=a->seat-1;
      a=a->next;
    }
  printf("seat deleted \n");
  return ;
}

No comments:

Post a Comment