Guest User

Untitled

a guest
May 20th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define LISTS 2
  6.  
  7. typedef struct car{
  8.     int id;
  9.     char manuName[20];
  10.     int year;
  11.     struct car *next;
  12. }Car;
  13.  
  14. void tempCar(Car* temp);
  15. Car* add(Car* head, Car* temp);
  16. Car* find(Car* head, int id);
  17. void freeList(Car* head);
  18. void printList(Car* head);
  19.  
  20. void main()
  21. {
  22.     int i;
  23.     Car* head[LISTS] = {NULL};
  24.     Car temp = {NULL};
  25.  
  26.     //Get The Cars -> Lists
  27.     printf("Please Enter Cars(-1 To Quit):\n");
  28.     printf("Car('Id, Manufacture Name(20 Chars), Year')\n");
  29.  
  30.     for(i=0;i<LISTS;i++)
  31.     {
  32.         printf("\nList[%d/%d]:\n", i+1, LISTS);
  33.         printf("-------------------------\n");
  34.         while(temp.id!=-1)
  35.         {
  36.             tempCar(&temp);
  37.             if(temp.id==-1)
  38.             {
  39.                 printf("-------------------------\n");
  40.                 break;
  41.             }
  42.             head[i] = add(head[i],&temp);
  43.         }
  44.         //Reset The Temp Car
  45.         temp.id = NULL;
  46.     }
  47.  
  48. }
  49.  
  50. void tempCar(Car* temp)
  51. {
  52.     printf("Car Id: ");
  53.     scanf("%4d",&temp->id);
  54.     if(temp->id==-1)
  55.         return;
  56.     flushall();
  57.     printf("Car Manufacture Name: ");
  58.     gets(temp->manuName);
  59.     printf("Car Manufacture Year: ");
  60.     scanf("%4d",&temp->year);
  61.     printf("-------------------------\n");
  62. }
  63.  
  64. Car* add(Car* head, Car* temp)
  65. {
  66.     Car* new_item;
  67.     new_item = (Car*) malloc(sizeof (Car));
  68.     if(new_item==NULL)
  69.     {
  70.         printf("No Memory!!!\n");
  71.         return NULL;
  72.     }
  73.     //Copy The Temp Car
  74.     new_item->id = temp->id;
  75.     strcpy(new_item->manuName, temp->manuName);
  76.     new_item->year = temp->year;
  77.     //Moving The Head
  78.     new_item->next = head;
  79.     return new_item;
  80. }
  81. void printList(Car* head)
  82. {
  83.     Car* to_print = head;
  84.     int i;
  85.     for(i=0;to_print != NULL;i++)
  86.     {
  87.         head = head->next;
  88.         //Car Print Start
  89.         printf("Car %d:\n",i+1);
  90.         printf("-------------------------\n");
  91.         printf("Car Id: %d \n",to_print->id);
  92.         printf("Car Manufacture Name: %s\n",to_print->manuName);
  93.         printf("Car Manufacture Year: %d\n",to_print->year);
  94.         printf("-------------------------\n");
  95.         //Car Print End
  96.         to_print = head;
  97.     }
  98. }
  99. Car* find(Car* head, int id)
  100. {
  101.     Car* curr = head;
  102.     while ((curr != NULL)  && (curr->id != id))
  103.         curr = curr->next;
  104.     return curr;
  105. }
  106. void freeList(Car* head)
  107. {
  108.     Car* to_free = head;
  109.     while (to_free != NULL)
  110.     {
  111.         head = head->next;
  112.         free(to_free);
  113.         to_free = head;
  114.     }
  115. }
Add Comment
Please, Sign In to add comment