0% found this document useful (0 votes)
2 views22 pages

Code

Hat hat dengu dengu dengu hat hat nikal laude

Uploaded by

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

Code

Hat hat dengu dengu dengu hat hat nikal laude

Uploaded by

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

Abstract:

The Ticket Booking System is a software application developed to simplify the process
of booking tickets for various events, transportation services, or movies. The main
objective of this project is to provide users with an efficient and user-friendly platform
for reserving tickets digitally instead of using manual methods. The system stores and
manages customer details, ticket availability, booking records, and cancellation
information effectively.

This project is implemented using Data Structures concepts to improve performance


and data management. Arrays, linked lists, stacks, queues, and trees can be used for
storing user data, managing seat allocation, maintaining waiting lists, and processing
bookings efficiently. Searching and sorting techniques are also applied to quickly
retrieve ticket information and organize booking records.

The Ticket Booking System reduces manual errors, saves time, and ensures accurate
transaction handling. It provides features such as ticket reservation, cancellation, seat
availability checking, and booking history management. The project demonstrates how
Data Structures play an important role in developing real-time applications with
efficient memory usage and faster processing.

Overall, this project helps in understanding the practical implementation of Data


Structures in solving real-world problems and improving the efficiency of ticket
management systems.

Algorithm:
Step 1: System Initialization and Data Structure Setup Allocate memory for the
primary hash table array. Set all bucket head pointers to NULL. Define total seat
capacity, hash table size, and the maximum booking record length as compile-
time constants.

Step 2: Seat Inventory Population Pre-populate the hash table with all available
seat records. Each seat is represented as a node with a seat number (key),
availability status (AVAILABLE or BOOKED), and passenger name field (empty
initially). Compute each seat's bucket index using a modulo hash function on the
seat number.

Step 3: User Interaction Loop Present an interactive menu to the user with the
following options:

1. Book a Ticket
2. Cancel a Booking
3. Check Seat Availability
4. View All Bookings
5. Exit

Step 4: Book a Ticket Prompt the user for a seat number and passenger name.
Validate that the seat number is within range. Compute its hash index and
traverse the linked list at that bucket. If the seat is found and its status is
AVAILABLE, update the node: set status to BOOKED, copy the passenger name,
and assign a unique booking ID. If already BOOKED, display a conflict warning
and return to the menu.

Step 5: Cancel a Booking Prompt the user for a booking ID or seat number.
Compute the hash index and traverse the chain. If the seat is found and status is
BOOKED, reset the status to AVAILABLE and clear the passenger name. If not
found or already available, display an appropriate error message.

Step 6: Check Seat Availability Prompt the user for a seat number. Compute the
hash index, traverse the chain, and print the current status (AVAILABLE or
BOOKED with the passenger name) to the terminal.

Step 7: View All Bookings Traverse all buckets in the hash table sequentially. For
each node with status BOOKED, print the booking ID, seat number, and
passenger name in a formatted table.

Step 8: Input Validation and Fault Handling Intercept blank inputs, out-of-range
seat numbers, and non-alphabetic names using pointer-check and character
validation routines. Trigger a runtime warning message and return to the menu
loop without corrupting internal state.

Step 9: Memory Deallocation and System Termination On exit, traverse every


bucket in the hash table and free all dynamically allocated nodes. Reset the
table pointer and flush I/O streams before closing the application cleanly.
Flowchart:
+-------------------------------------------------------------+

| [START] System Boot |

+-------------------------------------------------------------+

+-------------------------------------------------------------+

| Allocate Hash Table & Populate All Seat Inventory Nodes |

+-------------------------------------------------------------+

+-------------------------------------------------------------+

| Display Interactive Booking Menu |

+-------------------------------------------------------------+

/---------------------------------\

/ User Menu Choice? \

< >

\ /

\---------------------------------/

| | | | |

v v v v v

[1. Book] [2. Cancel] [3. Check Seat] [4. View All] [5. Exit]

| | | |
v v v v

Prompt Seat Prompt Seat Prompt Seat Loop All Buckets

& Passenger Number or ID Number Print BOOKED Nodes

| | |

v v v

Hash(Seat No.) Hash(Seat No.) Hash(Seat No.)

Find Node Find Node Find Node

| | |

/---------\ /---------\ /---------\

/ AVAILABLE?\ / BOOKED? \ / Status? \

< X X >

\ /\ /\ /

\---------/ \-----------/ \---------/

YES NO YES NO Print

| | | | Status

v | v |

Set Status | Set Status |

= BOOKED | = AVAILABLE |

Assign Name| Clear Name |

+ Book ID | |

| | |

v v v

Print Confirmation / Warning Message

+-------------------------------------------------------------+
| Return to Main Menu Loop |

+-------------------------------------------------------------+

[On Exit Choice]

+-------------------------------------------------------------+

| Free All Nodes, Flush Memory, Close Application |

+-------------------------------------------------------------+

[END]

Code:
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#define HASH_TABLE_SIZE 37

#define MAX_NAME_LENGTH 50

#define TOTAL_SEATS 20

#define AVAILABLE 0

#define BOOKED 1

// Node structure for each seat record (Separate Chaining)


typedef struct SeatNode {

int seat_number;

int status; // 0 = AVAILABLE, 1 = BOOKED

char passenger_name[MAX_NAME_LENGTH];

int booking_id;

struct SeatNode* next;

} SeatNode;

// Primary Hash Table

typedef struct HashTable {

SeatNode* buckets[HASH_TABLE_SIZE];

} HashTable;

// Global booking ID counter

int booking_counter = 1000;

// Hash function: maps seat number to bucket index

unsigned int compute_hash(int seat_number) {

return (unsigned int)(seat_number % HASH_TABLE_SIZE);

// Allocate and insert a new seat node

void insert_seat(HashTable* table, int seat_number) {

if (!table) return;
unsigned int index = compute_hash(seat_number);

SeatNode* new_node = (SeatNode*)malloc(sizeof(SeatNode));

if (!new_node) {

fprintf(stderr, "[FATAL ERROR]: Memory allocation failure for seat %d.\n",


seat_number);

return;

new_node->seat_number = seat_number;

new_node->status = AVAILABLE;

new_node->booking_id = -1;

new_node->passenger_name[0] = '\0';

new_node->next = table->buckets[index];

table->buckets[index] = new_node;

// Initialize hash table and populate all seats

HashTable* initialize_system() {

HashTable* table = (HashTable*)malloc(sizeof(HashTable));

if (!table) {

fprintf(stderr, "[FATAL ERROR]: Hash table allocation failed.\n");

exit(EXIT_FAILURE);

for (int i = 0; i < HASH_TABLE_SIZE; i++) {

table->buckets[i] = NULL;
}

for (int seat = 1; seat <= TOTAL_SEATS; seat++) {

insert_seat(table, seat);

return table;

// Lookup a seat node by seat number

SeatNode* find_seat(HashTable* table, int seat_number) {

if (!table) return NULL;

unsigned int index = compute_hash(seat_number);

SeatNode* current = table->buckets[index];

while (current != NULL) {

if (current->seat_number == seat_number) {

return current;

current = current->next;

return NULL;

// Book a ticket
void book_ticket(HashTable* table) {

int seat_number;

char name[MAX_NAME_LENGTH];

printf("Enter seat number (1-%d): ", TOTAL_SEATS);

if (scanf("%d", &seat_number) != 1 || seat_number < 1 || seat_number > TOTAL_SEATS)


{

printf("[WARNING]: Invalid seat number entered.\n");

while (getchar() != '\n');

return;

while (getchar() != '\n');

printf("Enter passenger name: ");

if (fgets(name, MAX_NAME_LENGTH, stdin) == NULL) return;

name[strcspn(name, "\n")] = '\0';

// Validate name contains at least one letter

int valid = 0;

for (int i = 0; name[i] != '\0'; i++) {

if (isalpha((unsigned char)name[i])) { valid = 1; break; }

if (!valid) {

printf("[WARNING]: Passenger name must contain alphabetic characters.\n");

return;

}
SeatNode* seat = find_seat(table, seat_number);

if (!seat) {

printf("[ERROR]: Seat %d not found in system.\n", seat_number);

return;

if (seat->status == BOOKED) {

printf("[CONFLICT]: Seat %d is already booked by %s (Booking ID: %d).\n",

seat_number, seat->passenger_name, seat->booking_id);

return;

seat->status = BOOKED;

seat->booking_id = booking_counter++;

strncpy(seat->passenger_name, name, MAX_NAME_LENGTH - 1);

seat->passenger_name[MAX_NAME_LENGTH - 1] = '\0';

printf("\n[SUCCESS]: Booking Confirmed!\n");

printf(" Seat Number : %d\n", seat->seat_number);

printf(" Passenger : %s\n", seat->passenger_name);

printf(" Booking ID : %d\n", seat->booking_id);

// Cancel a booking
void cancel_ticket(HashTable* table) {

int seat_number;

printf("Enter seat number to cancel (1-%d): ", TOTAL_SEATS);

if (scanf("%d", &seat_number) != 1 || seat_number < 1 || seat_number > TOTAL_SEATS)


{

printf("[WARNING]: Invalid seat number.\n");

while (getchar() != '\n');

return;

while (getchar() != '\n');

SeatNode* seat = find_seat(table, seat_number);

if (!seat) {

printf("[ERROR]: Seat %d not found.\n", seat_number);

return;

if (seat->status == AVAILABLE) {

printf("[INFO]: Seat %d is not currently booked. Nothing to cancel.\n",


seat_number);

return;

printf("[SUCCESS]: Booking cancelled for %s (Seat %d, Booking ID: %d).\n",

seat->passenger_name, seat->seat_number, seat->booking_id);


seat->status = AVAILABLE;

seat->booking_id = -1;

seat->passenger_name[0] = '\0';

// Check availability of a seat

void check_availability(HashTable* table) {

int seat_number;

printf("Enter seat number to check (1-%d): ", TOTAL_SEATS);

if (scanf("%d", &seat_number) != 1 || seat_number < 1 || seat_number > TOTAL_SEATS)


{

printf("[WARNING]: Invalid seat number.\n");

while (getchar() != '\n');

return;

while (getchar() != '\n');

SeatNode* seat = find_seat(table, seat_number);

if (!seat) {

printf("[ERROR]: Seat %d not found.\n", seat_number);

return;

printf("\n Seat %-4d : ", seat->seat_number);


if (seat->status == AVAILABLE) {

printf("AVAILABLE\n");

} else {

printf("BOOKED | Passenger: %-20s | Booking ID: %d\n",

seat->passenger_name, seat->booking_id);

// Display all booked tickets

void view_all_bookings(HashTable* table) {

printf("\n------------------------------------------------------------------\n");

printf(" ALL CURRENT BOOKINGS\n");

printf("------------------------------------------------------------------\n");

printf(" %-12s %-8s %-25s\n", "Booking ID", "Seat No.", "Passenger Name");

printf("------------------------------------------------------------------\n");

int found = 0;

for (int i = 0; i < HASH_TABLE_SIZE; i++) {

SeatNode* current = table->buckets[i];

while (current != NULL) {

if (current->status == BOOKED) {

printf(" %-12d %-8d %-25s\n",

current->booking_id, current->seat_number, current->passenger_name);

found = 1;

}
current = current->next;

if (!found) {

printf(" No bookings found.\n");

printf("------------------------------------------------------------------\n");

// Free all memory

void free_system(HashTable* table) {

if (!table) return;

for (int i = 0; i < HASH_TABLE_SIZE; i++) {

SeatNode* current = table->buckets[i];

while (current != NULL) {

SeatNode* temp = current;

current = current->next;

free(temp);

free(table);

// Main application loop


int main() {

HashTable* system = initialize_system();

int choice;

printf("\n*** TICKET BOOKING SYSTEM INITIALIZED ***\n");

printf(" Total Seats Available: %d\n", TOTAL_SEATS);

while (1) {

printf("\n========================================\n");

printf(" TICKET BOOKING SYSTEM\n");

printf("========================================\n");

printf(" 1. Book a Ticket\n");

printf(" 2. Cancel a Booking\n");

printf(" 3. Check Seat Availability\n");

printf(" 4. View All Bookings\n");

printf(" 5. Exit\n");

printf("========================================\n");

printf("Enter your choice: ");

if (scanf("%d", &choice) != 1) {

printf("[WARNING]: Please enter a valid numeric choice.\n");

while (getchar() != '\n');

continue;

while (getchar() != '\n');


switch (choice) {

case 1: book_ticket(system); break;

case 2: cancel_ticket(system); break;

case 3: check_availability(system); break;

case 4: view_all_bookings(system); break;

case 5:

printf("\nReleasing memory... System shutting down. Goodbye.\n");

free_system(system);

return 0;

default:

printf("[WARNING]: Invalid option. Please choose 1-5.\n");

APPROACH:
Methodology, Architectural Paradigm, and Software Engineering Specifications

The engineering architecture of this application follows a modular, decoupled


methodology divided into four distinct layers: Static Memory Structuring,
Deterministic Address Evaluation, Collision-Safe Chain Traversal, and Dynamic Heap
Memory Deallocation Lifecycle.

[Link] Data Structure — Hash Table with Separate Chaining

Rather than storing seat records in a flat array requiring O(N) linear searches, the
system maps each seat number to a hash table bucket using a modulo-based hash
function. Each bucket holds the head of a singly linked list, so multiple seat records
that hash to the same index (collisions) are chained without data corruption. This
guarantees O(1) average-case lookup, insertion, and deletion regardless of system
scale.

[Link] Function Design

The hash function uses integer modulo arithmetic on the seat number:

Hash(seat) = seat_number % HASH_TABLE_SIZE

The table size is set to a prime number (37) to distribute seat records evenly across
buckets and minimize clustering, which keeps linked-list chain depths close to 1 under
normal load.

[Link] and Cancellation Logic

Each seat node carries a status flag (AVAILABLE or BOOKED), a passenger name buffer,
and a unique booking ID assigned from a global counter. Booking checks the status flag
before writing, preventing double-bookings. Cancellation resets the flag and clears the
name buffer, making the seat immediately available for new reservations without any
structural reallocation.

[Link] Validation and Fault Handling

All user inputs pass through range checks (seat number within 1 to TOTAL_SEATS),
type validation (scanf return value checking), and alphabetic content verification for
names before any pointer operations are performed. This prevents segmentation
faults from out-of-bound hash indices and protects internal state from malformed
input.

[Link] Management

All seat nodes are dynamically allocated at initialization. On system exit, the
free_system() function traverses every bucket chain and individually deallocates each
node before freeing the table structure itself, eliminating all heap memory leaks.

output:
*** TICKET BOOKING SYSTEM INITIALIZED ***
Total Seats Available: 20

========================================

TICKET BOOKING SYSTEM

========================================

1. Book a Ticket

2. Cancel a Booking

3. Check Seat Availability

4. View All Bookings

5. Exit

========================================

Enter your choice: 1

Enter seat number (1-20): 5

Enter passenger name: Rahul Sharma

[SUCCESS]: Booking Confirmed!

Seat Number : 5

Passenger : Rahul Sharma

Booking ID : 1000

========================================

Enter your choice: 1

Enter seat number (1-20): 5

Enter passenger name: Priya Reddy


[CONFLICT]: Seat 5 is already booked by Rahul Sharma (Booking ID: 1000).

========================================

Enter your choice: 1

Enter seat number (1-20): 12

Enter passenger name: Anil Kumar

[SUCCESS]: Booking Confirmed!

Seat Number : 12

Passenger : Anil Kumar

Booking ID : 1001

========================================

Enter your choice: 3

Enter seat number to check (1-20): 7

Seat 7 : AVAILABLE

========================================

Enter your choice: 3

Enter seat number to check (1-20): 5

Seat 5 : BOOKED | Passenger: Rahul Sharma | Booking ID: 1000

========================================
Enter your choice: 4

------------------------------------------------------------------

ALL CURRENT BOOKINGS

------------------------------------------------------------------

Booking ID Seat No. Passenger Name

------------------------------------------------------------------

1000 5 Rahul Sharma

1001 12 Anil Kumar

------------------------------------------------------------------

========================================

Enter your choice: 2

Enter seat number to cancel (1-20): 5

[SUCCESS]: Booking cancelled for Rahul Sharma (Seat 5, Booking ID: 1000).

========================================

Enter your choice: 4

------------------------------------------------------------------

ALL CURRENT BOOKINGS

------------------------------------------------------------------

Booking ID Seat No. Passenger Name

------------------------------------------------------------------
1001 12 Anil Kumar

------------------------------------------------------------------

========================================

Enter your choice: 1

Enter seat number (1-20): 99

[WARNING]: Invalid seat number entered.

========================================

Enter your choice: 5

References:
1. Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed, “Fundamentals of Data
Structures in C”, Universities Press.

2. Reema Thareja, “Data Structures Using C”, Oxford University Press.

3. Yashavant Kanetkar, “Data Structures Through C”, BPB Publications.

4. Robert Lafore, “Data Structures and Algorithms in C++”, SAMS Publishing.

5. Mark Allen Weiss, “Data Structures and Algorithm Analysis in C”, Pearson Education.

6. TutorialsPoint – Data Structures Tutorial

TutorialsPoint Data Structures

7. GeeksforGeeks – Ticket Booking System Using Data Structures

GeeksforGeeks

8. Programiz – Data Structures Concepts

Programiz Data Structures

You might also like