Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Directions: Complete the following lab assignment using the description given in

ID: 3853887 • Letter: D

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 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

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <malloc.h>

#define MAX_MODEL 25

#define MAX_LINE 50

typedef struct Car {

char* model;

char* color;

int year;

struct Car* next;

} car;

car* readFromFile(FILE* fPtr);

car* insertInSortedOrder(car* head, char* model, char* color, int year);

car* createCar(char* model, char* color, int year);

int updateCar(car* head, char* targetModel, int year);

car* removeCar(car* head, char* targetModel);

void printCars(car* head);

void sort_cars();

typedef struct Car snode;

snode *newCar, *ptr, *prev, *temp;

snode *head = NULL, *last = NULL;

int main(int argc, char** argv){

FILE *fp;

fp = fopen("inventory.txt", "r");

readFromFile(fp);

//printing menu

while(1) {

printf("What would you like to do? 1)Add a car 2)Change a car 3)Delete a car 4)Quit? ");

int choice;

scanf("%d",&choice);

if(choice == 1) {

int year;

char model,color;

printf("Enter year: ");

scanf("%d",&year);

printf("Enter model: ");

scanf("%s",model);

printf("Enter color: ");

scanf("%s",color);

insertInSortedOrder(head,model,color,year);

}

else if(choice == 2) {

int year;

char model;

printf("Enter year: ");

scanf("%d",&year);

printf("Enter model: ");

scanf("%s",model);

updateCar(head, model, year);

}

else if(choice == 3) {

char model;

printf("Enter model: ");

scanf("%s",model);

removeCar(head, model);

}

else if(choice == 4) {

break;

}

else{

printf("Wrong choice ");

}

}

return 0;

}

car* readFromFile(FILE* fPtr) {

char line[1024];

int year;

char model,color;

while(fgets(line, sizeof(line), fp)){

fgets(line, 1024, fp);

sscanf(buffer, "%d, %s, %s", year, model,color);

insertInSortedOrder(head,model,color,year);

}

fclose(fp);

}

car* createCar(char* model, char* color, int year) {

newCar = (snode *)malloc(sizeof(snode));

if (newCar == NULL)

{

printf(" Memory was not allocated");

return 0;

}

else

{

newCar->model = model;

newCar->color = color;

newCar->year = year;

newCar->next = NULL;

return newCar;

}

}

void sort_cars()

{

snode *nxt;

int t;

char model;

char color;

if (head == NULL)

{

printf(" EMPTY LIST:");

}

else

{

for (ptr = head;ptr != NULL;ptr = ptr->next)

{

for (nxt = ptr->next;nxt != NULL;nxt = nxt->next)

{

if (ptr->year > nxt->year)

{

t = ptr->year;

model = ptr->model;

color = ptr->color;

ptr->year = nxt->year;

ptr->model = nxt->model;

ptr->color = nxt->color;

nxt->year = t;

nxt->model = model;

nxt->color = color;

}

}

}

}

}

car* insertInSortedOrder(car* head, char* model, char* color, int year) {

newCar = createCar(nodel,color,year);

if (head == last && last == NULL)

{

head = last = newCar;

head->next = NULL;

last->next = NULL;

}

else

{

last->next = newCar;

last = newCar;

last->next = NULL;

}

sort_cars();

}

int updateCar(car* head, char* targetModel, int year) {

int flag = 0;

if (head == NULL)

{

printf(":Empty list ");

}

else

{

for (ptr = head;ptr != NULL;ptr = ptr->next)

{

if (ptr->model == targetModel)

{

ptr->year = year;

flag = 1;

break;

}

}

if (flag == 1)

{

printf(" Updated...");

}

else

{

printf(" Car not found");

}

}

}

car* removeCar(car* head, char* targetModel) {

int position = 0;

if (head == NULL)

{

printf(":Empty list ");

}

else

{

for (ptr = head;ptr != NULL;ptr = ptr->next)

{

if (ptr->model == targetModel)

{

break;

}

position++;

}

}

if (position == 1)

{

head = ptr->next;

printf(" Car deleted");

}

else

{

if (position > 0)

{

ptr = head;

for (i = 1;i < position;i++)

{

prev = ptr;

ptr = ptr->next;

}

prev->next = ptr->next;

}

free(ptr);

}

}

void printCars(car* head) {

if (head == NULL)

{

ISEMPTY;

printf("Empty list ");

}

else

{

for (ptr = head;ptr != NULL;ptr = ptr->next)

{

printf("%d %s %s ", ptr->year,ptr->color,ptr-.model);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote