c语言餐饮排队叫号系统
时间: 2024-02-04 07:02:33 浏览: 102
排队叫号系统C语言程序
5星 · 资源好评率100%
以下是一个简单的c语言餐饮排队叫号系统的代码示例:
```c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct queue {
int id;
struct queue *next;
};
struct queue *head = NULL;
struct queue *tail = NULL;
int count = 0;
void enqueue(int id) {
struct queue *newNode = (struct queue*)malloc(sizeof(struct queue));
newNode->id = id;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
}
else {
tail->next = newNode;
tail = newNode;
}
count++;
printf("顾客 %d 已进入队列,当前队列长度为 %d\n", id, count);
}
void dequeue() {
if (head == NULL) {
printf("队列为空\n");
}
else {
struct queue *temp = head;
head = head->next;
free(temp);
count--;
printf("顾客 %d 已离开队列,当前队列长度为 %d\n", temp->id, count);
}
}
void display() {
if (head == NULL) {
printf("队列为空\n");
}
else {
printf("当前队列情况:\n");
struct queue *current = head;
while (current != NULL) {
printf("%d ", current->id);
current = current->next;
}
printf("\n");
}
}
int main() {
int choice, id;
while (1) {
printf("欢迎使用餐饮排队叫号系统!\n");
printf("请选择以下操作:\n");
printf("1. 进入队列\n");
printf("2. 离开队列\n");
printf("3. 显示队列\n");
printf("4. 退出系统\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入您的编号:\n");
scanf("%d", &id);
enqueue(id);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("欢迎再次使用!\n");
exit(0);
break;
default:
printf("输入有误,请重新输入!\n");
break;
}
}
return 0;
}
```
该示例使用链表实现了一个简单的队列,并提供了进入队列、离开队列和显示队列等基本操作,以实现餐饮排队叫号系统的功能。
阅读全文