0% found this document useful (0 votes)
9 views7 pages

Queues

This document contains a C program that implements a ticket booking system using a queue data structure. It allows users to book tickets, serve customers, and view the queue, with a maximum of 5 customers. The program includes functions for initializing the queue, checking if it is empty or full, and handling customer operations.

Uploaded by

Aditya Verma
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)
9 views7 pages

Queues

This document contains a C program that implements a ticket booking system using a queue data structure. It allows users to book tickets, serve customers, and view the queue, with a maximum of 5 customers. The program includes functions for initializing the queue, checking if it is empty or full, and handling customer operations.

Uploaded by

Aditya Verma
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
You are on page 1/ 7

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

#define MAX 5 // Maximum queue size

// Structure for Customer


typedef struct {
char name[50];
int ticketNumber;
} Customer;

// Queue Structure
typedef struct {
Customer queue[MAX];
int front, rear;
} TicketQueue;

// Function to initialize the queue


void initializeQueue(TicketQueue *q) {
q->front = -1;
q->rear = -1;
}

// Check if the queue is empty


int isEmpty(TicketQueue *q) {
return (q->front == -1);
}

// Check if the queue is full


int isFull(TicketQueue *q) {
return (q->rear == MAX - 1);
}

// Function to add a customer to the queue


void enqueue(TicketQueue *q, char name[], int
ticketNumber) {
if (isFull(q)) {
printf("\nQueue is full! No more bookings can be
taken.\n");
return;
}

if (isEmpty(q)) {
q->front = 0;
}

q->rear++;
strcpy(q->queue[q->rear].name, name);
q->queue[q->rear].ticketNumber = ticketNumber;

printf("\nBooking confirmed! %s got Ticket Number:


%d\n", name, ticketNumber);
}

// Function to serve a customer (dequeue)


void dequeue(TicketQueue *q) {
if (isEmpty(q)) {
printf("\nNo customers in queue!\n");
return;
}

printf("\nServing Customer: %s (Ticket No: %d)\n", q-


>queue[q->front].name, q->queue[q->front].ticketNumber);

if (q->front == q->rear) {
// Last customer served
initializeQueue(q);
} else {
q->front++;
}
}

// Function to display the queue


void displayQueue(TicketQueue *q) {
if (isEmpty(q)) {
printf("\nQueue is empty!\n");
return;
}

printf("\nCustomers in Queue:\n");
for (int i = q->front; i <= q->rear; i++) {
printf("%d. %s (Ticket No: %d)\n", i + 1, q-
>queue[i].name, q->queue[i].ticketNumber);
}
}

int main() {
TicketQueue queue;
initializeQueue(&queue);

int choice, ticketNumber = 1001;


char name[50];

while (1) {
printf("\n==== Ticket Booking System ====\n");
printf("1. Book a Ticket\n");
printf("2. Serve a Customer\n");
printf("3. View Queue\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
getchar(); // Consume newline character

switch (choice) {
case 1:
if (!isFull(&queue)) {
printf("Enter Customer Name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0'; // Remove
newline
enqueue(&queue, name, ticketNumber++);
} else {
printf("\nNo more tickets available!\n");
}
break;
case 2:
dequeue(&queue);
break;
case 3:
displayQueue(&queue);
break;
case 4:
printf("\nThank you for using the Ticket Booking
System!\n");
exit(0);
default:
printf("\nInvalid choice! Please enter a valid
option.\n");
}
}
return 0;
}

You might also like