Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- // Define a structure to represent a seat in the theater
- typedef struct node
- {
- char row_no; // Row number of the seat
- int seat_no,pin; // Seat number and unique pin code of the seat
- char book; // Book status (a for available, b for booked)
- struct node *next,*prev; // Pointers to next and previous nodes in the linked list
- }node;
- node *hn=NULL; // Head pointer for the linked list
- // Function to create the linked list of seats
- void create()
- {
- node *nn,*cn; // nn: new node, cn: current node
- int j=1;
- int k=2;
- char c ='A';
- // Loop to create nodes for each row of seats
- do
- {
- int i=1;
- // Loop to create nodes for each seat in a row
- do
- {
- k=(kk)/10+100-k/2; // Calculation to generate unique pin codes
- nn=(struct node)malloc(sizeof(node)); // Allocate memory for a new node
- nn->next=nn->prev=NULL;
- nn->row_no=c; // Assign row number to the node
- nn->seat_no=i; // Assign seat number to the node
- nn->pin=k; // Assign pin code to the node
- nn->book='a'; // Set the book status to available
- // If linked list is empty, make the new node the head node
- if(hn==NULL)
- {
- hn=nn;
- nn->next=nn->prev=hn;
- }
- // If linked list is not empty, add the new node to the end
- else
- {
- cn=hn;
- // Traverse the linked list to find the last node
- while(cn->next!=hn)
- cn=cn->next;
- // Add the new node to the end of the linked list
- cn->next=nn;
- nn->prev=cn;
- nn->next=hn;
- hn->prev=nn;
- }
- i++;
- }while(i<=7); // Repeat the loop for each seat in a row
- j++;
- c++;
- }while(j<=10); // Repeat the loop for each row of seats
- }
- // Function to display the seats with their availability status
- void display()
- {
- node *cn; // Current node
- cn=hn;
- // Display the header for Platinum section
- printf("------------------------------------------------------------------\n");
- printf("| Platinum |\n");
- // Traverse the linked list to display each seat
- while(cn->next!=hn) // cn->next is not equal to the head node
- {
- if((cn->prev)->row_no!=cn->row_no) // If the row number of the previous node is not equal to the current node
- printf("| "); // Print a "|"
- printf("%c ",cn->row_no); // Print the row number
- printf("%d ",cn->seat_no); // Print the seat number
- if(cn->book=='a') // If the seat is booked with 'a'
- printf("\033[32;40m -> a \033[0m"); // Print green text with black background
- else // If the seat is booked with 'b'
- printf("\033[1;31;43m -> b \033[0m"); // Print red text
- if((cn->next)->row_no!=cn->row_no) // If the row number of the next node is not equal to the current node
- printf("|\n"); // Print a "|" and a new line
- if(cn->row_no=='C'&&cn->seat_no==7) // If the row number is 'C' and the seat number is 7
- {
- printf("-----------------------------------------------------------------------\n"); // Print a separator line
- printf("| gold |\n"); // Print a "gold" category label
- }
- if(cn->row_no=='H'&&cn->seat_no==7) // If the row number is 'H' and the seat number is 7
- {
- printf("----------------------------------------------------------------------\n"); // Print a separator line
- printf("| Silver |\n"); // Print a "silver" category label
- }
- cn=cn->next; // Move to the next node
- }
- // Print the details of the last node
- printf("%c",cn->row_no);
- printf("%d",cn->seat_no);
- if(cn->book=='a')
- printf("\033[32;40m -> a \033[0m");
- else
- printf("\033[1;31;43m -> b \033[0m");
- printf("|\n");
- printf("------------------------------------------------------------------\n\n");
- printf("\033[1;33;49mPLATINUM-> 150 GOLD-> 100 SILVER->60\033[0m\n");
- }
- // Function to display the booking details after the seats have been successfully booked
- void display1(node *tmp[20],int n)
- {
- if(n != 1) // Check if any seats were actually booked
- {
- printf("------------------------------------------------------------------\n");
- printf("THANK YOU!\nYOU HAVE SUCCESFULLY BOOKED THE SEATS\n");
- for(int i = 1; i < n; i++)
- {
- printf("%c %d ", tmp[i]->row_no, tmp[i]->seat_no);
- printf(" PIN : %d \n", tmp[i]->pin);
- }
- printf("!!!!!!!!!!!!!!!!!!KEEP PIN SAFELY!!!!!!!!!!!!!!!!!!!\n");
- printf("PINS ARE REQUIRED AT THE TIME OF CANCELATION OF SEATS\n");
- printf("------------------------------------------------------------------\n");
- }
- }
- void book_seat() {
- node *cn, *temp[20];
- int n, z, flag;
- char row;
- int seat;
- int ch1;
- do {
- z = 1;
- // Ask user for number of tickets they want to buy
- printf("\nEnter the number of tickets you want to buy: ");
- scanf("%d", &n);
- // Ask user for the seat numbers
- printf("\nEnter Seat Numbers: \n");
- for (int i = 1; i <= n; i++) {
- printf("NO %d = ", i);
- scanf(" %c", &row);
- scanf("%d", &seat);
- // Check if seat is available and book it
- cn = hn;
- while (cn->next != hn) {
- if (cn->row_no == row && cn->seat_no == seat) {
- if (cn->book == 'a') {
- cn->book = 'b';
- temp[z] = cn;
- z++;
- } else {
- printf("INVALID CHOICE!\n");
- printf("%c%d Seat is already reserved\n", cn->row_no, cn->seat_no);
- }
- }
- cn = cn->next;
- }
- // Check if the last node is the required seat
- if (cn->row_no == row && cn->seat_no == seat) {
- if (cn->book == 'a') {
- cn->book = 'b';
- temp[z] = cn;
- z++;
- } else {
- printf("INVALID CHOICE!\n");
- printf("%c%d Seat is already reserved\n", cn->row_no, cn->seat_no);
- }
- }
- }
- // Display the seats that have been booked successfully
- display1(temp, z);
- // Ask user if they want to check seat status, book other seats, or exit
- printf("\n\nPress 1 to check seat status\n");
- printf("Press 2 to book other seats\n");
- printf("Press 3 to exit booking portal\n");
- printf("Input: ");
- scanf("%d", &ch1);
- if (ch1 == 1) {
- // display();
- }
- } while (ch1 == 2);
- }
- // Function to cancel a booked seat
- void cancel()
- {
- // variable to store the row number of the seat to be cancelled
- char row;
- // variable to store the user's choice of action after cancelling a seat
- int ch;
- // variables to store the seat number and pin of the seat to be cancelled
- int seat,pin;
- // pointer to traverse the linked list of seats
- node *cn;
- do
- {
- // initializing ch to 1
- ch=1;
- // setting cn to the head node of the linked list
- cn=hn;
- // Prompting the user to input the seat number
- printf("SEAT NUMBER :");
- // Reading the row number of the seat
- scanf(" %c",&row);
- // Reading the seat number
- scanf("%d",&seat);
- // Prompting the user to input the pin
- printf("PIN :");
- // Reading the pin
- scanf("%d",&pin);
- // Traversing the linked list of seats
- while(cn->next!=hn)
- {
- // If the seat number and pin match, ask the user for confirmation to cancel the seat
- if(cn->row_no==row && cn->seat_no==seat && cn->pin==pin)
- {
- printf("Are you sure u want to cancle the Seat (y/n) ");
- char c;
- scanf(" %c",&c);
- if(c=='y'||c=='Y')
- {
- printf("SEAT CANCELED SUCCESFULLY!\n");
- // changing the booking status of the seat to 'a' (available)
- cn->book='a';
- }
- }
- // If the seat number matches but the pin does not, display an error message
- else if(cn->row_no==row && cn->seat_no==seat && cn->pin!=pin)
- {
- printf("invalid SEAT NUMBER && PIN combination!!!!\n");
- }
- // moving to the next seat
- cn=cn->next;
- }
- // same as above but for the last seat in the linked list
- if(cn->row_no==row && cn->seat_no==seat && cn->pin==pin)
- {
- printf("Are you sure u want to cancle (y/n) ");
- char c;
- scanf("%c",&c);
- if(c=='y'||c=='Y')
- {
- printf("SEAT CANCELED SUCCESFULLY!\n");
- cn->book='a';
- }
- }
- else if(cn->row_no==row && cn->seat_no==seat && cn->pin!=pin)
- {
- printf("invalid SEAT NUMBER && PIN combination!!!!\n");
- }
- // Prompting the user to either check the seat status, cancel more seats, or exit the cancellation portal
- printf("\n\nPRESS 1 To Check Seat Status\n");
- printf("PRESS 2 To Cancle More Tickets\n
- printf("PRESS 3 To Exit CANCELATION PORTAL\n");
- printf("\ninput: ");
- scanf("%d",&ch);
- if(ch==1);
- // display();
- }
- while(ch==2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement