C Ticket Booking System: Linked List Movie Orders in C
C Ticket Booking System Using Linked Lists
This program manages movie ticket orders in C using a singly linked list. Pending orders are loaded from a file, new orders can be added, pending tickets can be issued to customers, and all changes are saved back to disk.
Ticket Structure
Each ticket is stored in a struct Ticket with the following fields:
- ticketID: unique identifier for the order
- customerID: identifier of the customer
- movieName: name of the movie
- seatsNumber: number of seats booked
- totalPrice: total price of the order
- next: pointer to the next ticket in the list
Loading Pending Orders
The getPending function reads pendingOrders.txt and builds the linked list one record at a time.
Displaying Pending Orders
The displayPending function prints every pending ticket order to the console.
Adding a New Ticket Order
The addTicket function asks the user for ticket details, inserts the new ticket at the head of the list, and appends it to pendingOrders.txt.
Issuing a Ticket
The paidTicket function searches for a ticket by its ID. When found, it removes the ticket from the pending list, appends it to PaidTickets.txt, frees its memory, and rewrites pendingOrders.txt with the remaining orders.
Saving and Exiting
The printPending function saves the current pending list to pendingOrders.txt. It is called when the user chooses to exit the program.
Main Menu
- Add a new ticket order
- Issue a ticket to a customer
- Display all pending ticket orders
- Exit the program
Source Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Ticket {
int ticketID;
char customerID[10];
char movieName[20];
int seatsNumber;
float totalPrice;
struct Ticket *next;
} Ticket;
void getPending(Ticket **head) {
FILE *file = fopen("pendingOrders.txt", "r");
if (!file) return;
Ticket *newTicket, *last = NULL;
while (!feof(file)) {
newTicket = (Ticket *)malloc(sizeof(Ticket));
if (fscanf(file, "%d %s %s %d %f", &newTicket->ticketID, newTicket->customerID, newTicket->movieName, &newTicket->seatsNumber, &newTicket->totalPrice) != 5) {
free(newTicket);
break;
}
newTicket->next = NULL;
if (!(*head)) {
*head = newTicket;
} else {
last->next = newTicket;
}
last = newTicket;
}
fclose(file);
}
void displayPending(Ticket *head) {
Ticket *temp = head;
while (temp) {
printf("%d %s %s %d %.2f\n", temp->ticketID, temp->customerID, temp->movieName, temp->seatsNumber, temp->totalPrice);
temp = temp->next;
}
}
void addTicket(Ticket **head) {
Ticket *newTicket = (Ticket *)malloc(sizeof(Ticket));
printf("Enter ticketID: ");
scanf("%d", &newTicket->ticketID);
printf("Enter customerID: ");
scanf("%s", newTicket->customerID);
printf("Enter movieName: ");
scanf("%s", newTicket->movieName);
printf("Enter seatsNumber: ");
scanf("%d", &newTicket->seatsNumber);
printf("Enter totalPrice: ");
scanf("%f", &newTicket->totalPrice);
newTicket->next = *head;
*head = newTicket;
FILE *file = fopen("pendingOrders.txt", "a");
if (file) {
fprintf(file, "%d %s %s %d %.2f\n", newTicket->ticketID, newTicket->customerID, newTicket->movieName, newTicket->seatsNumber, newTicket->totalPrice);
fclose(file);
}
}
void paidTicket(Ticket **head, int ticketID) {
Ticket *temp = *head, *prev = NULL;
while (temp && temp->ticketID != ticketID) {
prev = temp;
temp = temp->next;
}
if (!temp) return; // Ticket not found
if (prev) {
prev->next = temp->next;
} else {
*head = temp->next;
}
FILE *file = fopen("PaidTickets.txt", "a");
if (file) {
fprintf(file, "%d %s %d %.2f\n", temp->ticketID, temp->customerID, temp->seatsNumber, temp->totalPrice);
fclose(file);
}
free(temp);
file = fopen("pendingOrders.txt", "w");
temp = *head;
while (temp) {
fprintf(file, "%d %s %s %d %.2f\n", temp->ticketID, temp->customerID, temp->movieName, temp->seatsNumber, temp->totalPrice);
temp = temp->next;
}
fclose(file);
}
void printPending(Ticket *head) {
FILE *file = fopen("pendingOrders.txt", "w");
Ticket *temp = head;
while (temp) {
fprintf(file, "%d %s %s %d %.2f\n", temp->ticketID, temp->customerID, temp->movieName, temp->seatsNumber, temp->totalPrice);
temp = temp->next;
}
fclose(file);
}
int main() {
Ticket *head = NULL;
int choice, ticketID;
getPending(&head);
while (1) {
printf("1 - Add a new ticket order.\n");
printf("2 - Issue a ticket to a customer.\n");
printf("3 - Display all pending ticket orders.\n");
printf("0 - Exit the program.\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addTicket(&head);
break;
case 2:
printf("Enter ticketID to issue: ");
scanf("%d", &ticketID);
paidTicket(&head, ticketID);
break;
case 3:
displayPending(head);
break;
case 0:
printPending(head);
return 0;
default:
printf("Invalid choice!\n");
}
}
return 0;
}