用c语言银行队列系统,C语言学习之用队列实现银行排队系统
时间: 2024-06-10 10:04:55 浏览: 66
好的,我了解了你的问题。使用C语言实现银行队列系统需要用到队列的数据结构。具体实现步骤可以如下:
1. 包含头文件
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
```
2. 定义结构体
银行队列系统需要对每一个客户进行管理,因此我们需要定义一个客户结构体。每个客户包含以下信息:
```c
// 客户结构体
typedef struct customer {
int id; // 客户编号
char name[20]; // 客户姓名
int age; // 客户年龄
char gender[10]; // 客户性别
char id_card[20]; // 客户身份证号
char mobile[20]; // 客户手机号
int priority; // 优先级
struct customer *next; // 队列指针
} Customer;
```
3. 定义队列和相关操作
我们需要定义一个队列结构体,包含队头和队尾指针。每个队列还有两个操作:入队和出队。
```c
// 队列结构体
typedef struct queue {
Customer *front; // 队头指针
Customer *rear; // 队尾指针
} Queue;
// 创建队列
Queue *createQueue();
// 入队
void enQueue(Queue *queue, Customer *customer);
// 出队
Customer *deQueue(Queue *queue);
// 判断队列是否为空
int isEmpty(Queue *queue);
```
4. 实现队列操作
```c
// 创建队列
Queue *createQueue() {
Queue *queue = (Queue *)malloc(sizeof(Queue));
if (queue) {
queue->front = queue->rear = NULL;
}
return queue;
}
// 入队
void enQueue(Queue *queue, Customer *customer) {
if (queue && customer) {
if (isEmpty(queue)) {
queue->front = queue->rear = customer;
} else {
queue->rear->next = customer;
queue->rear = customer;
}
printf("客户 %s 已进入队列,当前队列长度:%d\n", customer->name, queueLength(queue));
}
}
// 出队
Customer *deQueue(Queue *queue) {
if (queue && !isEmpty(queue)) {
Customer *customer = queue->front;
queue->front = queue->front->next;
printf("客户 %s 已出队,当前队列长度:%d\n", customer->name, queueLength(queue));
return customer;
}
return NULL;
}
// 判断队列是否为空
int isEmpty(Queue *queue) {
return (queue == NULL || queue->front == NULL || queue->rear == NULL);
}
// 队列长度
int queueLength(Queue *queue) {
int length = 0;
Customer *current = queue->front;
while (current) {
length++;
current = current->next;
}
return length;
}
```
5. 主函数实现
现在我们可以在主函数中测试队列操作是否正常,模拟银行排队过程。
```c
int main() {
Queue *queue = createQueue(); // 创建队列
// 创建客户并入队
Customer *customer1 = (Customer *)malloc(sizeof(Customer));
customer1->id = 1;
strcpy(customer1->name, "张三");
customer1->age = 30;
strcpy(customer1->gender, "男");
strcpy(customer1->id_card, "310101199001010011");
strcpy(customer1->mobile, "13800000001");
customer1->priority = 1;
customer1->next = NULL;
enQueue(queue, customer1);
Customer *customer2 = (Customer *)malloc(sizeof(Customer));
customer2->id = 2;
strcpy(customer2->name, "李四");
customer2->age = 25;
strcpy(customer2->gender, "女");
strcpy(customer2->id_card, "310101199501010022");
strcpy(customer2->mobile, "13800000002");
customer2->priority = 2;
customer2->next = NULL;
enQueue(queue, customer2);
Customer *customer3 = (Customer *)malloc(sizeof(Customer));
customer3->id = 3;
strcpy(customer3->name, "王五");
customer3->age = 50;
strcpy(customer3->gender, "男");
strcpy(customer3->id_card, "310101197001010033");
strcpy(customer3->mobile, "13800000003");
customer3->priority = 3;
customer3->next = NULL;
enQueue(queue, customer3);
// 出队
deQueue(queue);
deQueue(queue);
deQueue(queue);
return 0;
}
```
以上就是使用C语言实现银行队列系统的实现步骤。希望对你有所帮助!
阅读全文