fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. typedef struct
  6. {
  7. int seatnumber;
  8. char name[20];
  9. }PASSENGER;
  10.  
  11. struct node
  12. {
  13. int seatnumber;
  14. char name[20];
  15. struct node *link;
  16. };
  17.  
  18. typedef struct node*NODE;
  19. NODE getnode()
  20. {
  21. NODE x;
  22. x=(NODE)malloc(sizeof(struct node));
  23. if(x==NULL)
  24. {
  25. printf("out of memory\n");
  26. exit(0);
  27. }
  28. return x;
  29. }
  30.  
  31. NODE AddPassenger(PASSENGER item,NODE first)
  32. {
  33. NODE temp;
  34. temp=getnode();
  35. temp->seatnumber=item.seatnumber;
  36. strcpy(temp->name,item.name);
  37. temp->link=NULL;
  38. if(first==NULL)
  39. return temp;
  40. temp->link=first;
  41. return temp;
  42. }
  43.  
  44. NODE AddPassenger_last(PASSENGER item,NODE first)
  45. {
  46. NODE temp,cur;
  47. temp=getnode();
  48. temp->seatnumber=item.seatnumber;
  49. strcpy(temp->name,item.name);
  50. temp->link=NULL;
  51. if(first==NULL)
  52. return temp;
  53. cur=first;
  54. while(cur->link!=NULL)
  55. {
  56. cur=cur->link;
  57. }
  58. cur->link=temp;
  59. return first;
  60. }
  61.  
  62. NODE DeletePassenger(NODE first)
  63. {
  64. NODE temp;
  65. if(first==NULL)
  66. {
  67. printf("passenger list is empty\n");
  68. return NULL;
  69. }
  70. temp=first;
  71. temp=temp->link;
  72. printf("delete passenger record :seatnumber=%d\n",first->seatnumber);
  73. free(first);
  74. return temp;
  75. }
  76.  
  77. NODE DeletePassenger_last(NODE first)
  78. {
  79. NODE cur,prev;
  80. if(first==NULL)
  81. {
  82. printf("passenger list is empty cannot delete\n");
  83. return first;
  84. }
  85. if(first->link==NULL)
  86. {
  87. printf("delete passenger record:seatnumber=%d\n",first->seatnumber);
  88. free(first);
  89. return NULL;
  90. }
  91. prev=NULL;
  92. cur=first;
  93. while(cur->link!=NULL)
  94. {
  95. prev=cur;
  96. cur=cur->link;
  97. }
  98. printf("delete passenger record:seatnumber=%d\n",cur->seatnumber);
  99. free(cur);
  100. prev->link=NULL;
  101. return first;
  102. }
  103.  
  104. void display(NODE first)
  105. {
  106. NODE cur;
  107. int count=0;
  108. if(first==NULL)
  109. {
  110. printf("passenger list is empty\n");
  111. return;
  112. }
  113. cur=first;
  114. while(cur!=NULL)
  115. {
  116. printf("%d\t%s\t\n",cur->seatnumber,cur->name);
  117. cur=cur->link;
  118. count++;
  119. }
  120. printf("number of passengers=%d\n",count);
  121. }
  122.  
  123. void main()
  124. {
  125. NODE first;
  126. int choice;
  127. PASSENGER item;
  128. first=NULL;
  129. for(;;)
  130. {
  131. printf("1.Add passengers from first\n2.Add the passenger from last \n3.Delete the passengers from first \n4.Delete the passenger from last \n5.display the passengers\n6.exit\n");
  132. printf("Enter the choice\n");
  133. scanf("%d",&choice);
  134. switch(choice)
  135. {
  136. case 1:
  137. printf("Seat Number :\n");
  138. scanf("%d",&item.seatnumber);
  139. printf("name :\n");
  140. scanf("%s",item.name);
  141. first=AddPassenger(item,first);
  142. break;
  143. case 2:
  144. printf("Seat Number :\n");
  145. scanf("%d",&item.seatnumber);
  146. printf("name :\n");
  147. scanf("%s",item.name);
  148. first=AddPassenger_last(item,first);
  149. break;
  150. case 3:
  151. first=DeletePassenger(first);
  152. break;
  153. case 4:
  154. first=DeletePassenger_last(first);
  155. break;
  156. case 5:
  157. display(first);
  158. break;
  159. default:
  160. exit(0);
  161. }
  162. }
  163. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
1.Add passengers from first
2.Add the passenger from last 
3.Delete the passengers from first 
4.Delete the passenger from last 
5.display the passengers
6.exit
Enter the choice