Question: **MUST BE DONE IN C PROGRAMMING LANGUAGE** Program adds a car to a linked list in C Modify the given append_to_list function to do the
**MUST BE DONE IN C PROGRAMMING LANGUAGE**
Program adds a car to a linked list in C
Modify the given append_to_list function to do the following

Given Code:
struct car *append_to_list(struct car *list){
struct car* newCar = (struct car*)malloc(sizeof(struct car)); newCar->next = NULL;
printf("Enter Car Manufacturer : "); read_line(newCar->make, LEN); printf("Enter Model: "); read_line(newCar->model, LEN); printf("Enter Color: "); read_line(newCar->color, LEN); printf("Enter year: "); scanf("%d", &newCar->year); printf("Enter City MPG: "); scanf("%d", &newCar->city_mpg); printf("Enter Highway MPG: "); scanf("%d", &newCar->highway_mpg); printf("Enter Quantity: "); scanf("%d", &newCar->quantity);
if(list == NULL) { return newCar; } else { struct car* ptr = list; while(1) { if( (strcmp(ptr->make, newCar->make)==0) && (strcmp(ptr->model, newCar->model)==0) && (strcmp(ptr->color, newCar->color)==0) && (ptr->year == newCar->year) && (ptr->city_mpg == newCar->city_mpg) && (ptr->highway_mpg == newCar->highway_mpg) && (ptr->quantity == newCar->quantity) ) { printf("Car already exists in the list. "); return list; } if(ptr->next != NULL) { ptr = ptr->next; } else { break; } } ptr->next = newCar;
return list; }
return NULL; }
The cars will be inserted into an ordered list (by make, then model, then color, then year) and the list remains ordered after the insertion. An example ordering of a list of cars: ford escape blue 2010 ford escape blue 2012 ford escape blue 2017 ford escape red 2010 ford escape white 2014 ford taurus silver 2005 jeep wrangler black 2007 Jeep wrangler black 2017
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
