0% found this document useful (0 votes)
14 views

C Project Code

Uploaded by

gargvanshika001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

C Project Code

Uploaded by

gargvanshika001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

#define MAX_NAME_LENGTH 50
#define MAX_CLASS_LENGTH 20
#define FILENAME "passengers.csv" // Filename set to passengers.csv

// Define the Passenger struct


typedef struct Passenger {
char name[MAX_NAME_LENGTH];
char category[MAX_NAME_LENGTH]; // Can be "senior", "woman", or "other"
int seat_number; // Seat number
char class_of_travel[MAX_CLASS_LENGTH]; // Can be "first class" or "second
class"
} Passenger;

// Define the Node struct (linked list node)


typedef struct Node {
Passenger passenger;
struct Node* next;
} Node;

// Define the Headers struct (multi-header linked list)


typedef struct Headers {
Node* senior_head;
Node* woman_head;
Node* other_head;
} Headers;

// Function to initialize headers (all headers to NULL)


void init_headers(Headers* headers) {
headers->senior_head = NULL;
headers->woman_head = NULL;
headers->other_head = NULL;
}

// Function to create a new node


Node* create_node(Passenger p) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->passenger = p;
new_node->next = NULL;
return new_node;
}

// Function to insert a passenger into the list and adjust headers


void insert_passenger(Headers* headers, Passenger p) {
Node* new_node = create_node(p);
Node** insert_head;

// Determine which category the passenger belongs to and set the insert head
if (strcmp(p.category, "senior") == 0) {
insert_head = &headers->senior_head;
} else if (strcmp(p.category, "woman") == 0) {
insert_head = &headers->woman_head;
} else if (strcmp(p.category, "other") == 0) {
insert_head = &headers->other_head;
} else {
return; // Invalid category, do not insert
}

// Insert the new node at the end of the list


if (*insert_head == NULL) {
*insert_head = new_node;
} else {
Node* current = *insert_head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
}
}

// Function to dequeue a passenger from a specific list


Passenger dequeue_passenger(Node** head) {
Passenger p;
Node* temp_node = *head;

if (temp_node != NULL) {
*head = temp_node->next;
p = temp_node->passenger;
free(temp_node);
}
return p;
}

// Function to handle the boarding process


void boarding_process(Headers* headers) {
int alternate = 0; // Variable to alternate between gates

printf("Starting boarding process...\n");

// Continue until all headers are empty


while (headers->senior_head != NULL || headers->woman_head != NULL || headers-
>other_head != NULL) {
if (alternate == 0) {
// Gate 1 process
if (headers->senior_head != NULL) {
Passenger p = dequeue_passenger(&headers->senior_head);
printf("Gate 1: Senior Citizen %s (Seat %d, Class: %s)\n", p.name,
p.seat_number, p.class_of_travel);
} else if (headers->woman_head != NULL) {
Passenger p = dequeue_passenger(&headers->woman_head);
printf("Gate 1: Woman %s (Seat %d, Class: %s)\n", p.name,
p.seat_number, p.class_of_travel);
} else {
// Upgrade from Gate 2 if Gate 1 is empty
if (headers->other_head != NULL) {
Passenger p = dequeue_passenger(&headers->other_head);
printf("Gate 1 (upgraded): %s (Seat %d, Class: %s)\n", p.name,
p.seat_number, p.class_of_travel);
}
}
} else {
// Gate 2 process
if (headers->other_head != NULL) {
Passenger p = dequeue_passenger(&headers->other_head);
printf("Gate 2: %s (Seat %d, Class: %s)\n", p.name, p.seat_number,
p.class_of_travel);
}
}
// Alternate gates
alternate = !alternate;
}

printf("Boarding process completed.\n");


}

// Function to load passenger data from a CSV file


int load_passenger_data(const char* filename, Headers* headers) {
FILE* file = fopen(filename, "r");
if (!file) {
printf("Failed to open file: %s\n", filename);
return 0; // Failure
}

char line[256];
while (fgets(line, sizeof(line), file)) {
Passenger p;

// Parse the line into name, category, seat_number, and class_of_travel


sscanf(line, "%49[^,],%49[^,],%d,%19[^,\n]", p.name, p.category,
&p.seat_number, p.class_of_travel);

// Insert the passenger into the list


insert_passenger(headers, p);
}

fclose(file);
return 1; // Success
}

// Main function to demonstrate the implementation


int main() {
// Initialize headers
Headers headers;
init_headers(&headers);

// Load passenger data from a CSV file


if (!load_passenger_data(FILENAME, &headers)) {
printf("Error loading passenger data from file\n");
return 1; // Exit with error code
}

// Call the boarding process


boarding_process(&headers);

return 0;
}

You might also like