Railway_System_Linked_List_Report_C_Language
Railway_System_Linked_List_Report_C_Language
Lists
Implementing Linked List Data Structures in Railway Route Management
Date: [Date]
Table of Contents
1. Introduction
8. Simulation Example
Code Snippet:
```c
#include <stdio.h>
#include <stdlib.h>
struct Station {
char name[50];
struct Station* next;
};
struct Station* createStation(char* stationName) {
struct Station* newStation = (struct
Station*)malloc(sizeof(struct Station));
strcpy(newStation->name, stationName);
newStation->next = NULL;
return newStation;
}
```
Code Snippet:
```c
// Function to add a station to the route
void addStation(struct Station** head, char* stationName) {
struct Station* newStation = createStation(stationName);
if (*head == NULL) {
*head = newStation;
} else {
struct Station* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newStation;
}
}
// Function to display the route
void displayRoute(struct Station* head) {
struct Station* temp = head;
while (temp != NULL) {
printf("%s -> ", temp->name);
temp = temp->next;
}
printf("End\n");
}
```
Code Snippet:
```c
// Function to remove a station from the route
void removeStation(struct Station** head, char* stationName) {
if (*head == NULL) return;
if (temp->next != NULL) {
struct Station* toDelete = temp->next;
temp->next = temp->next->next;
free(toDelete);
}
}
```
Simulation Example
Complete example to simulate adding/removing stations and
displaying routes.
Code Snippet:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Station* route = NULL;
printf("Initial Route:\n");
displayRoute(route);
removeStation(&route, "Station B");
return 0;
}
```