Guest User

Untitled

a guest
May 21st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. newEqualList#include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define LISTS 3
  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. Car* newEqualList(Car* head1, Car* head2);
  20.  
  21. void main()
  22. {
  23.     int i;
  24.     Car* head[LISTS] = {NULL};
  25.     Car temp = {NULL};
  26.  
  27.     //Get The Cars -> Lists
  28.     printf("Please Enter Cars(-1 To Quit):\n");
  29.     printf("Car('Id, Manufacture Name(20 Chars), Year')\n");
  30.  
  31.     for(i=0;i<LISTS-1;i++)
  32.     {
  33.         printf("\nList[%d/%d]:\n", i+1, LISTS-1);
  34.         printf("-------------------------\n");
  35.         while(temp.id!=-1)
  36.         {
  37.             tempCar(&temp);
  38.             if(temp.id==-1)
  39.             {
  40.                 printf("-------------------------\n");
  41.                 break;
  42.             }
  43.             head[i] = add(head[i],&temp);
  44.         }
  45.         //Reset The Temp Car
  46.         temp.id = NULL;
  47.     }
  48.     head[2] = newEqualList(head[0],head[1]);
  49.     printList(head[2]);
  50.  
  51.     //Free Lists
  52.     for(i=0;i<LISTS;i++)
  53.     {
  54.         freeList(head[i]);
  55.     }
  56. }
  57.  
  58. void tempCar(Car* temp)
  59. {
  60.     printf("Car Id: ");
  61.     scanf("%4d",&temp->id);
  62.     if(temp->id==-1)
  63.         return;
  64.     flushall();
  65.     printf("Car Manufacture Name: ");
  66.     gets(temp->manuName);
  67.     printf("Car Manufacture Year: ");
  68.     scanf("%4d",&temp->year);
  69.     printf("-------------------------\n");
  70. }
  71.  
  72. Car* add(Car* head, Car* temp)
  73. {
  74.     Car* new_item;
  75.     new_item = (Car*) malloc(sizeof (Car));
  76.     if(new_item==NULL)
  77.     {
  78.         printf("No Memory!!!\n");
  79.         return NULL;
  80.     }
  81.     //Copy The Temp Car
  82.     new_item->id = temp->id;
  83.     strcpy(new_item->manuName, temp->manuName);
  84.     new_item->year = temp->year;
  85.     //Moving The Head
  86.     new_item->next = head;
  87.     return new_item;
  88. }
  89. void printList(Car* head)
  90. {
  91.     Car* to_print = head;
  92.     int i;
  93.     for(i=0;to_print != NULL;i++)
  94.     {
  95.         head = head->next;
  96.         //Car Print Start
  97.         printf("Car %d:\n",i+1);
  98.         printf("-------------------------\n");
  99.         printf("Car Id: %d \n",to_print->id);
  100.         printf("Car Manufacture Name: %s\n",to_print->manuName);
  101.         printf("Car Manufacture Year: %d\n",to_print->year);
  102.         printf("-------------------------\n");
  103.         //Car Print End
  104.         to_print = head;
  105.     }
  106. }
  107. Car* find(Car* head, int id)
  108. {
  109.     Car* curr = head;
  110.     while ((curr != NULL)  && (curr->id != id))
  111.         curr = curr->next;
  112.     return curr;
  113. }
  114. void freeList(Car* head)
  115. {
  116.     Car* to_free = head;
  117.     while (to_free != NULL)
  118.     {
  119.         head = head->next;
  120.         free(to_free);
  121.         to_free = head;
  122.     }
  123. }
  124. Car* newEqualList(Car* head1, Car* head2)
  125. {
  126.     Car* head[3];
  127.     Car* to_search;
  128.     Car* found;
  129.  
  130.     head[0] = head1;
  131.     head[1] = head2;
  132.     head[2] = NULL;
  133.     to_search = head[0];
  134.  
  135.     while (to_search != NULL)
  136.     {
  137.         head[0] = head[0]->next;
  138.         found = find(head[1], to_search->id);
  139.         head[2] = add(head[2], found);
  140.         to_search = head[0];
  141.     }
  142.     return head[2];
  143. }
Add Comment
Please, Sign In to add comment