Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- newEqualList#include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define LISTS 3
- typedef struct car{
- int id;
- char manuName[20];
- int year;
- struct car *next;
- }Car;
- void tempCar(Car* temp);
- Car* add(Car* head, Car* temp);
- Car* find(Car* head, int id);
- void freeList(Car* head);
- void printList(Car* head);
- Car* newEqualList(Car* head1, Car* head2);
- void main()
- {
- int i;
- Car* head[LISTS] = {NULL};
- Car temp = {NULL};
- //Get The Cars -> Lists
- printf("Please Enter Cars(-1 To Quit):\n");
- printf("Car('Id, Manufacture Name(20 Chars), Year')\n");
- for(i=0;i<LISTS-1;i++)
- {
- printf("\nList[%d/%d]:\n", i+1, LISTS-1);
- printf("-------------------------\n");
- while(temp.id!=-1)
- {
- tempCar(&temp);
- if(temp.id==-1)
- {
- printf("-------------------------\n");
- break;
- }
- head[i] = add(head[i],&temp);
- }
- //Reset The Temp Car
- temp.id = NULL;
- }
- head[2] = newEqualList(head[0],head[1]);
- printList(head[2]);
- //Free Lists
- for(i=0;i<LISTS;i++)
- {
- freeList(head[i]);
- }
- }
- void tempCar(Car* temp)
- {
- printf("Car Id: ");
- scanf("%4d",&temp->id);
- if(temp->id==-1)
- return;
- flushall();
- printf("Car Manufacture Name: ");
- gets(temp->manuName);
- printf("Car Manufacture Year: ");
- scanf("%4d",&temp->year);
- printf("-------------------------\n");
- }
- Car* add(Car* head, Car* temp)
- {
- Car* new_item;
- new_item = (Car*) malloc(sizeof (Car));
- if(new_item==NULL)
- {
- printf("No Memory!!!\n");
- return NULL;
- }
- //Copy The Temp Car
- new_item->id = temp->id;
- strcpy(new_item->manuName, temp->manuName);
- new_item->year = temp->year;
- //Moving The Head
- new_item->next = head;
- return new_item;
- }
- void printList(Car* head)
- {
- Car* to_print = head;
- int i;
- for(i=0;to_print != NULL;i++)
- {
- head = head->next;
- //Car Print Start
- printf("Car %d:\n",i+1);
- printf("-------------------------\n");
- printf("Car Id: %d \n",to_print->id);
- printf("Car Manufacture Name: %s\n",to_print->manuName);
- printf("Car Manufacture Year: %d\n",to_print->year);
- printf("-------------------------\n");
- //Car Print End
- to_print = head;
- }
- }
- Car* find(Car* head, int id)
- {
- Car* curr = head;
- while ((curr != NULL) && (curr->id != id))
- curr = curr->next;
- return curr;
- }
- void freeList(Car* head)
- {
- Car* to_free = head;
- while (to_free != NULL)
- {
- head = head->next;
- free(to_free);
- to_free = head;
- }
- }
- Car* newEqualList(Car* head1, Car* head2)
- {
- Car* head[3];
- Car* to_search;
- Car* found;
- head[0] = head1;
- head[1] = head2;
- head[2] = NULL;
- to_search = head[0];
- while (to_search != NULL)
- {
- head[0] = head[0]->next;
- found = find(head[1], to_search->id);
- head[2] = add(head[2], found);
- to_search = head[0];
- }
- return head[2];
- }
Add Comment
Please, Sign In to add comment