Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add a delete function in car .c that delete a car from the list. The function should delete a player by the cars make, model,

Add a delete function in car .c that delete a car from the list. The function should delete a player by the cars make, model, color, and manufacture year . These information will be entered by the user. The function should have the following prototype:

struct car * delete_from_list(struct car *list);

You will also need to add the function prototype to the header file; modify the main function in dealer.c to add d for delete option in the menu and it calls the delete function when the doption is selected.

2)

Modify the append_to_list function so the car is inserted into an ordered list (by make, then model) and the list remains ordered after the insertion. For example,

ford escape should be before ford taurus in the list; ford taurus should be before jeep wrangler in the list; if the make and model are the same, the car entries do not need to be ordered by other members.

car.c

#include "car.h" // all of the functions besides main and readline are here

// The user enters the cars specifics and i save it to a list. struct car *append_to_list(struct car *list) { struct car * c = (struct car *) malloc(sizeof(struct car)); struct car *last = NULL, *curr = list; int found = 0; printf(" Enter the car's make, model, color, manufacture year, city, mpg, highway mpg, and quantity in that order: "); read_line(c->make, LEN); read_line(c->model, LEN); read_line(c->color, LEN); scanf("%d %d %d %d", &c->year, &c->city_mpg, &c->highway_mpg, &c->quantity); c -> next = NULL; if( list == NULL ) return c; else { //check if there is a duplicate here while(curr != NULL) { if(strcmp(curr -> make, c -> make) == 0 && strcmp(curr -> model, c -> model) == 0 && strcmp(curr -> color, c -> color) == 0 && curr -> year == c -> year) { found = 1; break; } if(curr -> next == NULL) last = curr; curr = curr -> next; } if( !found ) { last -> next = c; } else printf("Car with similar values exist in the list "); return list; } }

// this allows user to search for a specific car in the list. void find_car(struct car * list) { char make[LEN + 1], model[LEN + 1]; struct car *c = list; printf("Enter make: "); read_line(make, LEN); printf("Enter model: "); read_line(model, LEN); printf("%15s %15s %15s %15s %15s " , "color", "year", "city mpg", "highway mpg" , "quantity"); while(c != NULL) { if(strcmp(c->make, make) == 0 && strcmp(c -> model, model) == 0) { printf("%15s %15d %15d %15d %15d " , c -> color, c -> year, c -> city_mpg, c -> highway_mpg, c -> quantity); } c = c -> next; } printf("---------------------------------------------------------------- "); }

// prints the current list of cars void printList(struct car *list) { struct car *c = list; printf("%15s %15s %15s %15s " , "make", "model", "year", "color" ); while(c != NULL) { printf("%15s %15s %15d %15s " , c -> make, c -> model, c -> year , c -> color ); c = c -> next; } printf("---------------------------------------------------------------- "); }

// clearing the list by setting it to a new struct of temp void clearList(struct car *list) { struct car *c = list, *temp; while(c != NULL) { temp = c -> next; free(c); c = temp; } }

car.h

/* Here I put my struct for car and all my function declarations */ #ifndef CAR_H #define CAR_H #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);

#endif

dealer.c

#include #include #include #include #include "readline.h" #include "car.h"

/********************************************************** * main: Prompts the user to enter an operation code, * * then calls a function to perform the requested * * action. Repeats until the user enters the * * command 'q'. Prints an error message if the user * * enters an illegal code. * **********************************************************/ 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(" "); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions