Question
C Program Linked list You are to implement each of the following functions using the data structure below. Explanation would be helpful #define MAX_MODEL 25
C Program Linked list
You are to implement each of the following functions using the data structure below. Explanation would be helpful
#define MAX_MODEL 25
#define MAX_LINE 50
typedef struct Car {
char* model;
char* color;
int year;
struct Car* next;
} car;
Input files:
cars.txt:
Tesla S|royal|2015| GMC Yukon|white|2005| Dodge Charger|lime|2010| Honda Civic|cobalt|1996| Nissan Titan|saddlewood|2008| Chevrolet Silverado|saddlewood|1999| Ford Focus|lime|2000| Toyota Tacoma|gunmetal|2009| Tesla X|black|2016| Nissan Maxima|white|2013| Ford Explorer|forest|1996| Chevrolet Corvette|cherry|1982| Tesla 3|cherry|2017| Nissan 370Z|royal|2011| Ford F150|cherry|2006| Honda Accord|white|2004| Mitsubishi Eclipse|black|2002| Toyota Tundra|cobalt|2017| Dodge Challenger|lemon|1970| Chevrolet Camaro|black|1966|
color.txt:
black cherry cobalt forest gunmetal lemon lime royal saddlewood white
car* readFromFile(FILE* fPtr)
Reads and parses lines from the input file, then calls insertInSortedOrder to add them to a linked list. Returns a pointer to the head of the linked list created in this way. fgets(), strtok(), and strcpy() will be useful here.
car* insertInSortedOrder(car* head, char* model, char* color, int year)
Uses createCar to make a new Car, then inserts that Car into the linked list beginning with head. The new Car should be inserted in ascending alphabetical order (A to Z) by model. Returns a pointer to the list's head. strcmp() will be useful here.
car* createCar(char* model, char* color, int year)
Creates a new Car that has the given model and year. This must be the only function in your program that uses malloc. If any other function uses malloc, you will lose 5 points.
int updateCar(car* head, char* targetModel, char* newModel)
Searches for a Car in the linked list beginning at head with the model targetModel. If found, the model for the Car should be replaced by newModel and 1 should be returned. If not found, -1 should be returned.
car* removeCar(car* head, char* targetModel)
Finds the Car with model targetModel and removes it from the linked list. Regardless of whether the Car is found in the list or not, this function always returns the head of the linked list. You may safely assume that at most one car has a model of targetModel, and end execution of the function after removing it.
char* verifyColor(FILE* cPtr, char* color)
Checks that a user-entered color matches one of the available colors from the colors.txt file. Returns the color string if the color is valid and the car can be added using insertInSortedOrder, else it loops until the user re-enters a valid color. If the color is invalid, this function prints out a list of valid colors. DO NOT hard-code the colors into your printf statement, i.e. your code should have a line that looks like printf(%s, , color);
void printCars(car* head)
Prints all the cars from the linked list. Output must be properly formatted (do not use tabs or spaces to fake the formatting!) or you will not receive points for this function.
int main(int argc, char** argv)
You should figure out how to call your functions in main to produce output matching the sample output. main will receive two command line arguments, which is the name of the input file and the name of the color file. Implement a simple error check in main that no car is newer than 2017.
NOTE: Only main, verifyColor, and printCars are allowed to use printf. No other function should produce any output. If they do, you will lose points for those functions.
$ ./a.out cars.txt colors.txt
The list of cars in inventory are:
1966 Chevrolet Camaro black
1982 Chevrolet Corvette cherry
1999 Chevrolet Silverado saddlewood
1970 Dodge Challenger lemon
2010 Dodge Charger lime
1996 Ford Explorer forest
2000 Ford Focus lime
2006 Ford F150 cherry
2005 GMC Yukon white
2004 Honda Accord white
1996 Honda Civic cobalt
2002 Mitsubishi Eclipse black
2011 Nissan 370Z royal
2013 Nissan Maxima white
2008 Nissan Titan saddlewood
2017 Tesla 3 cherry
2015 Tesla S royal
2016 Tesla X black
2009 Toyota Tacoma gunmetal
2017 Toyota Tundra cobalt
What would you like to do? A)dd a car C)hange a car D)elete a car Q)uit? X
Invalid response. Try again.
What would you like to do? A)dd a car C)hange a car D)elete a car Q)uit? A
Enter a model name to ADD: Ford Focus
That model already exists. Try again.
Enter a model name to ADD: Jeep Wrangler
Enter a model year: 2017
Enter a color: red
Invalid color! Please pick from the following options:
black, cherry, cobalt, forest, gunmetal, lemon, lime, royal, saddlewood, white,
Enter a valid color: gunmetal
The list of cars in inventory are:
1966 Chevrolet Camaro black
1982 Chevrolet Corvette cherry
1999 Chevrolet Silverado saddlewood
1970 Dodge Challenger lemon
2010 Dodge Charger lime
1996 Ford Explorer forest
2000 Ford Focus lime
2006 Ford F150 cherry
2005 GMC Yukon white
2004 Honda Accord white
1996 Honda Civic cobalt
2017 Jeep Wrangler gunmetal
2002 Mitsubishi Eclipse black
2011 Nissan 370Z royal
2013 Nissan Maxima white
2008 Nissan Titan saddlewood
2017 Tesla 3 cherry
2015 Tesla S royal
2016 Tesla X black
2009 Toyota Tacoma gunmetal
2017 Toyota Tundra cobalt
What would you like to do? A)dd a car C)hange a car D)elete a car Q)uit? C
Enter a model name to CHANGE: Subaru Impreza
That model does not exist. Try again.
Enter a model name to CHANGE: Ford F150
Enter the new model year: 2019
Invalid year! The model year must be 2017 or older.
Enter the new model year: 2014
The list of cars in inventory are:
1966 Chevrolet Camaro black
1982 Chevrolet Corvette cherry
1999 Chevrolet Silverado saddlewood
1970 Dodge Challenger lemon
2010 Dodge Charger lime
1996 Ford Explorer forest
2000 Ford Focus lime
2014 Ford F150 cherry
2005 GMC Yukon white
2004 Honda Accord white
1996 Honda Civic cobalt
2017 Jeep Wrangler gunmetal
2002 Mitsubishi Eclipse black
2011 Nissan 370Z royal
2013 Nissan Maxima white
2008 Nissan Titan saddlewood
2017 Tesla 3 cherry
2015 Tesla S royal
2016 Tesla X black
2009 Toyota Tacoma gunmetal
2017 Toyota Tundra cobalt
What would you like to do? A)dd a car C)hange a car D)elete a car Q)uit? D
Enter a model name to DELETE: Lamborghini Aventador
That model does not exist. Try again.
Enter a model name to DELETE: Mitsubishi Eclipse
The list of cars in inventory are:
1966 Chevrolet Camaro black
1982 Chevrolet Corvette cherry
1999 Chevrolet Silverado saddlewood
1970 Dodge Challenger lemon
2010 Dodge Charger lime
1996 Ford Explorer forest
2000 Ford Focus lime
2014 Ford F150 cherry
2005 GMC Yukon white
2004 Honda Accord white
1996 Honda Civic cobalt
2017 Jeep Wrangler gunmetal
2011 Nissan 370Z royal
2013 Nissan Maxima white
2008 Nissan Titan saddlewood
2017 Tesla 3 cherry
2015 Tesla S royal
2016 Tesla X black
2009 Toyota Tacoma gunmetal
2017 Toyota Tundra cobalt
What would you like to do? A)dd a car C)hange a car D)elete a car Q)uit? Q
Exiting program
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started