/******************************************************************************
Linked list A2 v6
*******************************************************************************/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
enum Tire { SOMMERDAEK, VINTERDAEK };
enum Material { STAAL, ALU };
//struct that holds car data
//Using typedef to make the struct a datatype
typedef struct car{
char *regNumber;
char *carBrand;
enum Tire tire;
enum Material material;
int timeLeft;
struct car *nextCarPtr;
} Car;
//typedef struct car Car;
typedef Car *CarPtr;
void addCarFunction(CarPtr *firstPtr, CarPtr *lastPtr, char *newRegNumber, char *newCarBrand, char *newTire, char *newMaterial) {
//Allocating memory for the current car
CarPtr newCarPtr = malloc(sizeof(Car));
printf("addCarFunction received: %s\n", newRegNumber);
if (newCarPtr != NULL) {
//Inserts given values into car struct
newCarPtr -> regNumber = newRegNumber;
newCarPtr -> carBrand = newCarBrand;
//Converting values to enum
if (newTire == "sommerdæk") {
newCarPtr -> tire = SOMMERDAEK;
}
//if "sommerdæk" is spelled wrong it will be replaced with "vinterdæk". There is no error handling here.
else {
newCarPtr -> tire = VINTERDAEK;
}
if (newMaterial == "alu") {
newCarPtr -> material = ALU;
}
//Similar to before. There is no error handling here.
else {
newCarPtr -> material = STAAL;
}
//Calculating wait time
if (newCarPtr -> tire == SOMMERDAEK && newCarPtr -> material == ALU){
newCarPtr -> timeLeft = 20;
}
else if (newCarPtr -> tire == SOMMERDAEK && newCarPtr -> material == STAAL){
newCarPtr -> timeLeft = 18;
}
else {
newCarPtr -> timeLeft = 15;
}
//Placing current car in the back of the line using pointers
newCarPtr -> nextCarPtr = NULL;
//Checking if the queue is empty
if (*firstPtr == NULL) {
*firstPtr = newCarPtr;
}
else {
// *lastPtr needs to be in () because else the dereferencing will not work
// If not empty, the currently last known car is set to point to the next car
(*lastPtr) -> nextCarPtr = newCarPtr;
}
// The currently last known pointer is set to the new car
*lastPtr = newCarPtr;
}
//If no more memory print an error message.
else {
printf("%s Could not be added to the queue. There is no more memory left", newRegNumber);
}
}
char *removeFirstCarFunction(CarPtr *firstPtr, CarPtr *lastPtr) {
// Saves the reg number of the first car in the queue
char *removeRegNumber = (*firstPtr) -> regNumber;
// Saving the pointer to the first car in the queue
CarPtr carToBeRemovedPtr = *firstPtr;
// Set the pointer to the second car in the queue to be the first car in the queue
*firstPtr = (*firstPtr) -> nextCarPtr;
//Checking if the queue is empty
if (*firstPtr == NULL) {
*lastPtr = NULL;
}
//Freeing up memory
free(carToBeRemovedPtr);
return removeRegNumber;
}
void printQueueFunction(CarPtr thisCarPtr) {
printf("\n -------Printing queue Start------- \n");
if (thisCarPtr != NULL) {
// Continue until end of queue
while(thisCarPtr != NULL) {
printf("\n [%s]",thisCarPtr -> regNumber);
printf(" [%s]",thisCarPtr -> carBrand);
if (thisCarPtr -> tire == SOMMERDAEK) {
printf(" [sommerdæk]");
}
else {
printf(" [vinterdæk]");
}
if (thisCarPtr -> material == ALU) {
printf(" [alu]");
}
else {
printf(" [stål]");
}
// Move to the next car in the queue
thisCarPtr = thisCarPtr -> nextCarPtr;
}
}
else {
// If the queue is empty
printf("\nThere is no cars in the queue.");
}
printf("\n\n -------Printing queue End------- \n");
return;
}
void receiveCarInfoFunction(CarPtr *firstPtr, CarPtr *lastPtr) {
char inputRegNumber[7];
char inputBrand[15];
char *outputTire;
char *outputMaterial;
int inputTire;
int inputMaterial;
printf("Enter car reg number:\n");
scanf("%s", inputRegNumber);
printf("You entered: %s\n", inputRegNumber);
// outputRegNumber = inputRegNumber + '\0';
// printf("Converted: %s\n", outputRegNumber);
printf("Enter car brand:\n");
scanf("%s", inputBrand);
printf("Enter 0 for 'sommerdæk' or 1 for 'vinterdæk':\n");
scanf("%d", &inputTire);
printf("Enter 0 for 'stål' rims or 1 for 'alu' rims:\n");
scanf("%d", &inputMaterial);
if (inputTire == 0) {
outputTire = "sommerdæk";
}
else {
outputTire = "vinterdæk";
}
if (inputMaterial == 0) {
outputMaterial = "stål";
}
else {
outputMaterial = "alu";
}
addCarFunction(firstPtr, lastPtr, inputRegNumber, inputBrand, outputTire, outputMaterial);
}
char* receiveRegInfoFunction(CarPtr *firstPtr, CarPtr *lastPtr) {
char *inputRegNumber;
printf("Enter car reg number:\n");
scanf("%s", inputRegNumber);
return inputRegNumber;
}
void optionsFunction(CarPtr *firstPtr, CarPtr *lastPtr) {
unsigned int choice = 0;
while(choice != 12) {
printf("Enter your choice: \n"
" 1 to add a car to the queue\n"
" 2 to remove the first car from the queue\n"
" 3 to search for a specific car\n"
" 4 to get the waittime for a specific car\n"
" 5 to get information on how many cars need tire change on steel rims\n"
" 6 to get information on how many cars need tire change on alu rims\n"
" 7 to find queue position for a specific car\n"
" 8 to get information on how many cars of a specific brand is in the queue\n"
" 9 to remove a specific car from the queue\n"
" 10 to print all information on the cars in the queue\n"
" 11 to get information on how long all the cars in the queue\n"
" 12 to exit the program\n");
scanf("%u", &choice);
switch(choice) {
case 1:
receiveCarInfoFunction(firstPtr, lastPtr);
break;
case 2:
removeFirstCarFunction(firstPtr, lastPtr);
break;
case 3:
printf("case 3");
break;
case 4:
printf("case 4");
break;
case 5:
printf("case 5");
break;
case 6:
printf("case 6");
break;
case 7:
printf("case 7");
break;
case 8:
printf("case 8");
break;
case 9:
printf("case 9");
break;
case 10:
printQueueFunction(*firstPtr);
break;
case 11:
printf("case 11");
break;
}
}
}
void readFileFunction(CarPtr *firstPtr, CarPtr *lastPtr) {
FILE *inputFilePtr;
char *readRegNumber;
char *readCarBrand;
char *readTire;
char *readMaterial;
inputFilePtr = fopen("input.txt", "r");
char buffer[35]; // decide the buffer size as per your requirements.
while((fgets(buffer, 128, inputFilePtr))!= NULL) {
printf("%s", buffer);
char *tokenPtr = strtok(buffer, " ");
int i = 0;
while(tokenPtr != NULL) {
printf("%s\n", tokenPtr);
switch(i) {
case 0:
readRegNumber = tokenPtr;
printf("%s\n", readRegNumber);
break;
case 1:
//strcpy(readCarBrand, tokenPtr);
readCarBrand = tokenPtr;
break;
case 2:
readTire = tokenPtr;
break;
case 3:
readMaterial = tokenPtr;
break;
}
i++;
tokenPtr = strtok(NULL, " ");
}
addCarFunction(firstPtr, lastPtr, readRegNumber, readCarBrand, readTire, readMaterial);
}
fclose(inputFilePtr); //Close the file
}
int main() {
CarPtr firstPtr = NULL;
CarPtr lastPtr = NULL;
printf("Started...\n");
char testString[7] = "BB44556";
char *testStringPtr = "Ford";
readFileFunction(&firstPtr, &lastPtr);
addCarFunction(&firstPtr, &lastPtr, "AB11523", "Citroen", "vinterdæk", "alu");
//printQueue(firstPtr);
addCarFunction(&firstPtr, &lastPtr, "AA22523", testStringPtr, "sommerdæk", "stål");
//printQueue(firstPtr);
addCarFunction(&firstPtr, &lastPtr, "AA33523", "Volvo", "sommerdæk", "alu");
addCarFunction(&firstPtr, &lastPtr, testString, "Suzuki", "sommerdæk", "stål");
//printf("Removed %s from the queue.", removeFirstCar(&firstPtr, &lastPtr));
printQueueFunction(firstPtr);
// optionsFunction(&firstPtr, &lastPtr);
return 0;
}
AA10888 VW sommerdæk stål
KD62366 Audi sommerdæk stål
AX21538 Ford vinterdæk stål
CN31223 VW vinterdæk stål
NN20214 Ford sommerdæk alu
UM21378 Ford vinterdæk stål
NB20215 Ford sommerdæk alu
AV32361 Tesla sommerdæk stål