C Program to implement Ticket Counter simulation using Queue
If you travel around the world. You need to book a ticket. Here, we implement the Ticket Counter simulation. You can add a ticket, get the ticket and view the available tickets of customers.
- · Queue size is defined.
- · Queue structure is created and values are initialised. Is_empty() and is_full() functions are created.
- · To insert the values, enqueue() function is used.
- · To delete the values, deque() function is used.
- · If you want to see the customers details, print() is written.
- · In the main() function, the choice of customer is received. According to the choice, the values are displayed.
Code:
#include <stdio.h>
#include <stdlib.h>
//defines the maximum size as 12
#define MAX 12
// a Queue structure is created
typedef struct {
int q_front,
q_rear;
int items[MAX];
} Queue;
// Initialise the values
void initQueue(Queue *q) {
q->q_front =
-1;
q->q_rear = -1;
}
// queue is full or not
int is_Full(Queue *q) {
return
(q->q_rear == MAX - 1);
}
// It is used to Check the queue is empty or not
int is_Empty(Queue *q) {
return
(q->q_front == -1 || q->q_front > q->q_rear);
}
// Insert the value to queue
void enqueue(Queue *q, int cust) {
if (is_Full(q)) {
printf("Queue is full! Customer %d cannot wait.\n", cust);
return;
}
if (q->q_front
== -1) q->q_front = 0;
q->items[++q->q_rear] = cust;
printf("Customer %d joined the queue.\n", cust);
}
// Dequeue is used to remove the element
void dequeue(Queue *q) {
if (is_Empty(q)) {
printf("No customers in queue.\n");
return;
}
printf("Customer %d got the ticket and left.\n",
q->items[q->q_front]);
q->q_front++;
}
void print(Queue *q) {
if (is_Empty(q)) {
printf("Queue is empty.\n");
return;
}
printf("The
elements in Current queue: ");
for (int i =
q->q_front; i <= q->q_rear; i++) {
printf("%d ", q->items[i]);
}
printf("\n");
}
int main() {
Queue q;
initQueue(&q);
int choice,
customer = 1;
while (1) {
printf("\n--- Ticket Counter Simulation ---\n");
printf("1. New Customer\n");
printf("2. Service Customer\n");
printf("3. Display Queue\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch
(choice) {
case 1:
enqueue(&q, customer++);
break;
case 2:
dequeue(&q);
break;
case 3:
print(&q);
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Output:
--- Ticket Counter Simulation ---
1. New Customer
2. Service Customer
3. Display Queue
4. Exit
Enter choice: 1
Customer 1 joined the queue.
--- Ticket Counter Simulation ---
1. New Customer
2. Service Customer
3. Display Queue
4. Exit
Enter choice: 1
Customer 2 joined the queue.
--- Ticket Counter Simulation ---
1. New Customer
2. Service Customer
3. Display Queue
4. Exit
Enter choice: 2
Customer 1 got the ticket and left.
--- Ticket Counter Simulation ---
1. New Customer
2. Service Customer
3. Display Queue
4. Exit
Enter choice: 3
The elements in Current queue: 2 3
That’s it. The C Program to implement the Ticket Counter
Simulation in Queue was Successful. Keep Coding!!!
Comments
Post a Comment