Question
Directions: Complete the following lab assignment using the description given in each section. Be sure to comment your program; otherwise you may lose up to
Directions:
Complete the following lab assignment using the description given in each section. Be sure to comment your program; otherwise you may lose up to 5 points. You must use proper indent styles; otherwise you may lose up to 5 points.
Purpose:
Understand linked lists.
Understand how to insert to, update, and delete from linked lists.
Submission information:
Submit this homework assignment through the tc.rnet server using the submit command. It must be submitted before the due date and time. Not doing so WILL result in a zero, no exceptions.
Description:
You are to implement each of the following functions using the data structure below.
No global variables should be used in this lab! If you use global variables you will automatically lose 15 points.
#define MAX_MODEL 25
#define MAX_LINE 50
typedef struct Car {
char* model;
char* color;
int year;
struct Car* next;
} car;
Input files:
See Blackboard.
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, int year)
Searches for a Car in the linked list beginning at head with the model targetModel. If found, the year for the Car should be replaced by year 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 user-inputted year is newer than 2017. Also, you MUST figure out how to do your own memory freeing. This can either occur in main, or in a function you write yourself.
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
General
If your program does not compile, results in a segmentation fault, gets stuck in an infinite loop, or does not produce any input/output (I/O) because most of the source code is commented out then your lab will receive a grade of zero. You may receive partial credit if your C program compiles and produces some valid I/O that meets the lab specifications.
Remember to have a header at the top with your name, pawprint, assignment, etc.
8 points removeCar
8 points insertInSortedOrder
8 points updateCar
5 points main
5 points printCars
3 points verifyColor
3 points createCar
Use of global variables: -15 points
Use of malloc outside of createCar: -5 points
Use of printf outside of main and printCars: -5 points
Memory leaks: -20% PER BLOCK
Lack of internal comments/header: -10 points
BONUS (5 points)
Allow duplicate models and sort them based on Model then Year:
2008 Nissan Maxima cherry
2013 Nissan Maxima white
2008 Nissan Titan saddlewood
2017 Tesla 3 cherry
2015 Tesla S royal
2016 Tesla S forest
2016 Tesla X black
2001 Toyota Tacoma white
2009 Toyota Tacoma gunmetal
2017 Toyota Tundra cobalt
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