#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Passenger {
int survived;
int pclass;
int sex;
float age;
int sibSp;
int parch;
float fare;
int embarked;
};
struct Node {
struct Passenger passenger;
struct Node* next;
};
struct Node* createNode(struct Passenger passenger) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->passenger = passenger;
newNode->next = NULL;
return newNode;
}
void append(struct Node** headRef, struct Passenger passenger) {
struct Node* newNode = createNode(passenger);
struct Node* last = *headRef;
if (*headRef == NULL) {
*headRef = newNode;
return;
}
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}
void destroyList(struct Node** headRef) {
struct Node* current = *headRef;
struct Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
*headRef = NULL;
}
void readData(struct Node** headRef) {
FILE* file = fopen("titanic.csv", "r");
char line[1024];
if (file == NULL) {
printf("Error opening file.\n");
return;
}
fgets(line, sizeof(line), file); // Skip the header line
while (fgets(line, sizeof(line), file)) {
struct Passenger passenger;
char* token;
token = strtok(line, ",");
passenger.survived = atoi(token);
token = strtok(NULL, ",");
passenger.pclass = atoi(token);
token = strtok(NULL, ",");
if (strcmp(token, "male") == 0) {
passenger.sex = 1;
}
else {
passenger.sex = 0;
}
token = strtok(NULL, ",");
passenger.age = atof(token);
token = strtok(NULL, ",");
passenger.sibSp = atoi(token);
token = strtok(NULL, ",");
passenger.parch = atoi(token);
token = strtok(NULL, ",");
passenger.fare = atof(token);
token = strtok(NULL, ",");
if (strcmp(token, "S") == 0) {
passenger.embarked = 0;
}
else if (strcmp(token, "C") == 0) {
passenger.embarked = 1;
}
else if (strcmp(token, "Q") == 0) {
passenger.embarked = 2;
}
append(headRef, passenger);
}
fclose(file);
}
void printPassenger(struct Passenger passenger) {
char* sex = "";
if (passenger.sex == 1) {
sex = "male";
}
else {
sex = "female";
}
printf("Survived: %d, Class: %d, Sex: %s, Age: %.2f, SibSp: %d, Parch: %d, Fare: %.2f, Embarked: %d\n",
passenger.survived, passenger.pclass, sex, passenger.age, passenger.sibSp, passenger.parch, passenger.fare, passenger.embarked);
}
int main() {
struct Node* head = NULL;
readData(&head);
struct Node* current = head;
int counter = 0;
while (current != NULL) {
printf("Passenger %d:\n", counter + 1);
printPassenger(current->passenger);
counter++;
current = current->next;
}
destroyList(&head);
return 0;
}
In this C language code, we define structs for the Passenger and Node data types. The Passenger struct represents a single passenger with various attributes, and the Node struct represents a node in a linked list.
The functions createNode, append, and destroyList are used to create a new node, add a node to the end of the linked list, and free the memory allocated to the linked list, respectively.
In the main function, we call the readData function to populate the linked list with passenger data. We then traverse the linked list and print each passenger's details using the printPassenger function. Finally, we call the destroyList function to free the memory allocated to the linked list.