c语言银行排队管理系统链队列实现
时间: 2023-08-05 11:09:04 浏览: 90
下面是基于链队列实现银行排队管理系统的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // 队列的最大容量
// 队列节点结构体
typedef struct Node {
int id; // 客户编号
struct Node *next;
} Node;
// 队列结构体
typedef struct {
Node *front; // 队头指针
Node *rear; // 队尾指针
int length; // 队列长度
} Queue;
// 初始化队列
void InitQueue(Queue *queue) {
queue->front = queue->rear = (Node *) malloc(sizeof(Node));
queue->front->next = NULL;
queue->length = 0;
}
// 判断队列是否为空
int IsEmpty(Queue *queue) {
return queue->front == queue->rear;
}
// 入队
void EnQueue(Queue *queue, int id) {
Node *node = (Node *) malloc(sizeof(Node));
node->id = id;
node->next = NULL;
queue->rear->next = node;
queue->rear = node;
queue->length++;
}
// 出队
int DeQueue(Queue *queue) {
if (IsEmpty(queue)) {
printf("队列为空,无法出队!\n");
exit(1);
}
Node *node = queue->front->next;
int id = node->id;
queue->front->next = node->next;
if (queue->rear == node) {
queue->rear = queue->front;
}
free(node);
queue->length--;
return id;
}
// 输出队列中的所有元素
void PrintQueue(Queue *queue) {
if (IsEmpty(queue)) {
printf("队列为空!\n");
return;
}
printf("队列中的元素为:");
Node *node = queue->front->next;
while (node != NULL) {
printf("%d ", node->id);
node = node->next;
}
printf("\n");
}
int main() {
Queue queue;
InitQueue(&queue);
int choice, id;
while (1) {
printf("请输入操作代码:\n");
printf("1. 客户进入队列\n");
printf("2. 客户出队\n");
printf("3. 输出队列中的所有元素\n");
printf("4. 退出程序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
if (queue.length >= MAX_SIZE) {
printf("队列已满,无法进入!\n");
break;
}
printf("请输入客户编号:");
scanf("%d", &id);
EnQueue(&queue, id);
printf("客户 %d 进入队列!\n", id);
break;
case 2:
id = DeQueue(&queue);
printf("客户 %d 出队!\n", id);
break;
case 3:
PrintQueue(&queue);
break;
case 4:
printf("程序已退出!\n");
exit(0);
default:
printf("无效的操作代码!\n");
break;
}
}
return 0;
}
```
该程序中,我们使用了链表来实现队列,每个节点表示一个客户,包含客户的编号和指向下一个节点的指针。队列结构体中包含队头指针、队尾指针和队列长度三个成员变量。程序中实现了入队、出队、判断队列是否为空、输出队列中的所有元素等操作。在主函数中,我们通过一个循环来不断接受用户输入的操作代码,根据不同的操作代码执行相应的操作。