A fully interactive C-based Train Reservation System built around a weighted graph data structure. The system models a real-world railway network using an adjacency list, finds the shortest route between any two stations using Dijkstra's algorithm, and supports complete ticket lifecycle management — booking, viewing, cancellation, and modification.
- Highlights
- Screenshots
- Project Structure
- Data Structures & Algorithms Used
- Features & Functionalities
- Stations in the Network
- Trains Available
- Requirements
- How to Run
- How to Fork & Contribute
- Design Notes
- Future Improvements
- License
- Author
- 🗺️ Graph-based Station Network — 8 stations modeled as a weighted graph with an adjacency list.
- 🔍 Dijkstra's Shortest Path — Finds the optimal route between any two stations with full route reconstruction.
- 🎫 Ticket Booking — Book tickets with automatic seat allocation, unique ticket IDs, and fare calculation.
- 📋 View All Tickets — List all active bookings with complete details (passenger, train, route, fare).
- ❌ Ticket Cancellation — Cancel any ticket by ID and free up the reserved seat.
- ✏️ Ticket Modification — Change passenger name, source, or destination (cancels old ticket and creates a new one).
- 💰 Dynamic Fare Calculation — Fare = shortest path distance × 2 (rate per unit distance).
- 🚂 Multiple Trains — 4 different trains (Rajdhani Express, Shatabdi Express, Vande Bharat, Duranto Express) with different routes and seat capacities.
- 📊 Real-time Seat Availability — Shows available seats per train per route in real-time.
- 🔄 Interactive Menu-Driven Interface — Simple and intuitive CLI menu for all operations.
When the program starts, it displays the Train Reservation System banner followed by the interactive menu with 6 options. Here the user selects Option 1 — View all stations and routes, which displays:
- All 8 stations in the network (Delhi, Agra, Jaipur, Kanpur, Udaipur, Lucknow, Ahmedabad, Varanasi) with their index numbers.
- The complete adjacency list showing every route connection and its distance (weight). For example, Delhi connects to Jaipur (280 km) and Agra (200 km). This is the underlying graph structure that powers the shortest-path routing.
The user selects Option 2 — Book a new ticket. The booking flow works as follows:
- Enter source and destination stations — The user enters
Delhias source andJaipuras destination. - Available trains are displayed — The system shows all trains that service this route with their real-time seat availability (e.g., Rajdhani Express: 15/15 seats, Vande Bharat: 18/18 seats).
- Select a train and enter passenger name — User selects Train 2 (Vande Bharat) and enters
Shivansh. - Booking confirmation — A unique ticket
#1001is generated with full details: passenger name, train ID, seat number, source, destination, distance (280 units), fare (₹560), and the computed shortest route (Delhi -> Jaipur). - Book another ticket — The system asks if the user wants to book more. Here the user books a second ticket
#1002for passengerAlphafromKanpurtoDelhi. The system finds the shortest routeKanpur -> Agra -> Delhi(480 units, fare ₹960).
This screenshot demonstrates two operations in sequence:
- Booking another ticket — A third ticket
#1003is booked forBetafromKanpurtoDelhi(Seat 2 on Train 3). Notice the seat number increments automatically. - View all tickets (Option 3) — Lists all 3 active tickets in a clean tabular format:
#1001— Shivansh, Train 3, Seat 1, Delhi → Jaipur, Fare: 560#1002— Alpha, Train 3, Seat 1, Kanpur → Delhi, Fare: 960#1003— Beta, Train 3, Seat 2, Kanpur → Delhi, Fare: 960
- Cancel a ticket (Option 4) — The user cancels ticket
#1003. The system confirms the cancellation and frees up the seat.
This screenshot shows the full cancel → verify → modify workflow:
- Cancellation result — Ticket
#1003is confirmed as cancelled. - View remaining tickets (Option 3) — After cancellation, only 2 tickets remain (
#1001Shivansh and#1002Alpha). - Modify a ticket (Option 5) — The user modifies ticket
#1002:- The system displays the current ticket details (Passenger: Alpha, From: Kanpur to Delhi, Fare: 960).
- The user changes the passenger name to
betaand destination toAgra(source stays as Kanpur). - The system cancels the old ticket
#1002and creates a new ticket#1004with updated details. - New ticket shows: Passenger
beta, Kanpur → Agra, Distance: 280 units, Fare: 560, Route:Kanpur -> Agra.
The final state of the system:
- View all tickets (Option 3) — Shows the 2 remaining active tickets after all operations:
#1001— Shivansh, Train 3, Seat 1, Delhi → Jaipur, Fare: 560#1004— beta, Train 3, Seat 1, Kanpur → Agra, Fare: 560
- Exit (Option 6) — The user selects option 6 to exit the program gracefully.
train_reservation_system/
├── include/
│ ├── graph.h # Graph data structure definitions (stations, edges, Dijkstra)
│ ├── booking.h # Booking system definitions (trains, tickets, reservation)
│ └── route_query.h # Route query helper definitions
├── src/
│ ├── graph.c # Graph implementation (adjacency list, Dijkstra's algorithm)
│ ├── booking.c # Booking logic (ticket CRUD, seat allocation, fare calculation)
│ ├── route_query.c # Route query implementation
│ └── main.c # Main program entry point with interactive menu
├── Screenshots/
│ ├── 01_menu_and_stations.png
│ ├── 02_booking_tickets.png
│ ├── 03_view_and_cancel_tickets.png
│ ├── 04_cancel_and_modify_ticket.png
│ └── 05_final_tickets_and_exit.png
├── Makefile # Build configuration for Linux/Mac
├── train_reservation.exe # Pre-built Windows executable
└── README.md # This file
| Concept | Implementation | Purpose |
|---|---|---|
| Weighted Graph | Adjacency list (EdgeNode linked list per station) |
Models the railway network with stations as vertices and routes as weighted edges |
| Dijkstra's Algorithm | Vertex-scan based implementation | Finds the shortest path between any two stations |
| Path Reconstruction | Predecessor array (prev[]) |
Rebuilds the complete route from source to destination |
| Arrays | Fixed-size arrays for trains, tickets, seats | Stores trains, manages bookings, tracks seat occupancy |
| Linked List | Adjacency list edges | Each station maintains a linked list of its connections |
| Structs | Graph, Station, EdgeNode, Train, Ticket, ReservationSystem |
Encapsulates related data into clean C structures |
- Displays all 8 stations in the railway network with their index numbers.
- Shows the complete adjacency list — every station's connections with distances.
- Helps users understand the available routes before booking.
- Enter source and destination stations.
- System runs Dijkstra's algorithm to find the shortest path.
- Displays all trains available for the selected route with real-time seat availability.
- User selects a train and provides passenger name.
- System allocates a seat, calculates the fare (distance × 2), and issues a unique ticket with a full route breakdown.
- Option to book multiple tickets in one session.
- Error handling for invalid stations, no path found, train not found, or train full.
- Lists all active tickets in a clean tabular format.
- Shows ticket ID, passenger name, train, seat number, route, and fare.
- Cancel any ticket by entering its ticket ID.
- The reserved seat is freed and becomes available for future bookings.
- Confirmation message displayed on success.
- Enter the ticket ID to modify.
- System shows current ticket details (passenger, route, fare).
- User can change passenger name, source station, and/or destination station (press Enter to keep current values).
- The old ticket is cancelled and a new ticket is created with updated details.
- New fare is recalculated based on the new route.
- Gracefully exits the program with a goodbye message.
- Frees all allocated memory (graph adjacency lists).
| Index | Station |
|---|---|
| 0 | Delhi |
| 1 | Agra |
| 2 | Jaipur |
| 3 | Kanpur |
| 4 | Udaipur |
| 5 | Lucknow |
| 6 | Ahmedabad |
| 7 | Varanasi |
Delhi → Jaipur(280), Agra(200)
Agra → Jaipur(240), Kanpur(280), Delhi(200)
Jaipur → Agra(240), Udaipur(390), Delhi(280)
Kanpur → Varanasi(330), Lucknow(90), Agra(280)
Udaipur → Ahmedabad(260), Jaipur(390)
Lucknow → Varanasi(300), Kanpur(90)
Ahmedabad → Udaipur(260)
Varanasi → Kanpur(330), Lucknow(300)
| Train ID | Train Name | Total Seats | Stations Covered |
|---|---|---|---|
| 1 | Rajdhani Express | 15 | Delhi, Agra, Jaipur, Udaipur, Ahmedabad |
| 2 | Shatabdi Express | 12 | Delhi, Agra, Kanpur, Lucknow, Varanasi |
| 3 | Vande Bharat | 18 | All 8 stations (full network) |
| 4 | Duranto Express | 14 | Delhi, Kanpur, Lucknow, Varanasi, Agra |
- GCC (or any C11-compatible compiler)
- Make (optional — you can compile manually)
- Git (for cloning and contributing)
git clone https://github.com/shivanshpap/Train-Reservation-System.git
cd train_reservation_systemgcc -Wall -Wextra -std=c11 -Iinclude -g src/graph.c src/booking.c src/route_query.c src/main.c -o train_reservation.exe
.\train_reservation.exemake
./train_reservationgcc -Wall -Wextra -std=c11 -Iinclude -g src/graph.c src/booking.c src/route_query.c src/main.c -o train_reservation
./train_reservationmake clean
makeIf you don't have GCC installed, you can directly run the pre-built executable:
.\train_reservation.exe- Go to the repository page on GitHub.
- Click the Fork button in the top-right corner.
- This creates a copy of the repository under your GitHub account.
git clone https://github.com/shivanshpap/Train-Reservation-System.git
cd train_reservation_systemgit checkout -b feature/your-feature-nameEdit the source files in src/ and include/ directories.
# Windows
gcc -Wall -Wextra -std=c11 -Iinclude -g src/graph.c src/booking.c src/route_query.c src/main.c -o train_reservation.exe
.\train_reservation.exe
# Linux/Mac
make
./train_reservationgit add .
git commit -m "Add: description of your changes"
git push origin feature/your-feature-name- Go to your fork on GitHub.
- Click "Compare & pull request".
- Write a clear description of your changes.
- Submit the pull request for review.
# Add the original repo as upstream (one-time setup)
git remote add upstream https://github.com/shivanshpap/Train-Reservation-System.git
# Fetch and merge updates
git fetch upstream
git merge upstream/main- Adjacency List is used because the station network is sparse — most stations connect to only 2–3 others.
- Dijkstra's Algorithm uses a simple vertex scan (O(V²)), which is efficient enough for this small 8-station network and keeps the code easy to understand.
- Modular Architecture — Graph/routing logic lives in
graph.c, booking/ticket handling is inbooking.c, and the interactive menu is inmain.c. This clean separation makes the codebase easy to extend. - Ticket Modification works by cancelling the old ticket and booking a new one, ensuring data consistency and correct seat management.
- Fare Model is simple:
fare = shortest_path_distance × 2. This can be easily extended to support different fare classes.
- Replace vertex scan with a min-heap for faster Dijkstra (O(E log V)).
- Add timetable support for train departures and arrivals.
- Persist data to a file or database so tickets survive program restarts.
- Support multiple seat classes (Sleeper, AC, First Class) with different fare rates.
- Add date/time validation for bookings.
- Implement user authentication and account management.
- Add payment processing integration.
- Build a GUI frontend (GTK or web-based).
This project is open-source and available for educational purposes. Feel free to use, modify, and distribute.
Shivansh
If you found this project helpful, give it a ⭐ on GitHub!




