Question: Modify the append_to_list function for dealer.c program: The cars will be inserted into an ordered list (by make, then model, then color, then year) and
Modify the append_to_list function for dealer.c program: 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
Code for dealer.c program:
#include
#include
#include
#include
#define LEN 30
struct car{
char make[LEN+1];
char model[LEN+1];
char color[LEN+1];
int year;
int city_mpg;
int highway_mpg;
int quantity;
struct car *next;
};
struct car *append_to_list(struct car *list);
void find_car(struct car *list);
void printList(struct car *list);
void clearList(struct car *list);
int read_line(char str[], int n);
int main(void)
{
char code;
struct car *car_list = NULL;
printf("Operation Code: a for appending to the list, f for finding a car, p for printing the list; q for quit. ");
for (;;)
{
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' '); /* skips to end of line */
switch (code)
{
case 'a': car_list = append_to_list(car_list);
break;
case 'f': find_car(car_list);
break;
case 'p': printList(car_list);
break;
case 'q': clearList(car_list);
return 0;
default: printf("Illegal code ");
}
printf(" ");
}
}
struct car *append_to_list(struct car *list)
{
struct car *newCar = (struct car*)malloc(sizeof(struct car));
newCar->next = NULL;
printf(" Enter Make: ");
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))
{
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;
}
void find_car(struct car * list)
{
char userModel[LEN+1], userMake[LEN+1];
printf(" Enter make to search for: ");
read_line(userMake, LEN);
printf(" Enter model to search for: ");
read_line(userModel, LEN);
int found = 0;
struct car* ptr = list;
while(ptr != NULL)
{
if((strcmp(ptr->make, userMake)==0) &&(strcmp(ptr->model, userModel)==0))
{
struct car *c = ptr;
printf(" Color: %s, Year: %d, City_mpg: %d, Highway_mpg: %d, Quantity: %d", c->color, c->year, c->city_mpg, c->highway_mpg,c->quantity);
found = 1;
}
ptr = ptr->next;
}
if(!found)
{
printf(" Car not found.");
}
}
void printList(struct car *list)
{
struct car* ptr = list;
printf(" Make\tModel\t Color\tYear\tCity\tHighway\tQuantity\t");
while(ptr != NULL)
{
struct car *c = ptr;
printf(" %s\t%-9s%s\t%d\t%d\t%d\t%d", c->make, c->model, c->color, c->year, c->city_mpg, c->highway_mpg, c->quantity);
ptr = ptr->next;
}
}
void clearList(struct car *list)
{
struct car* ptr = list;
while(ptr != NULL)
{
struct car *c = ptr;
ptr = ptr->next;
free(c);
}
}
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()));
str[i++] = ch;
while ((ch = getchar()) != ' ')
{
if (i < n)
str[i++] = ch;
}
str[i] = '\0';
return i;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
