c语言排队系统
时间: 2023-12-15 10:04:51 浏览: 42
排队系统可以用队列来实现。下面是一个简单的C语言排队系统代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 100
typedef struct {
int data[MAX_QUEUE_SIZE];
int front, rear;
} Queue;
void initQueue(Queue *queue) {
queue->front = queue->rear = 0;
}
int isQueueEmpty(Queue *queue) {
return queue->front == queue->rear;
}
int isQueueFull(Queue *queue) {
return (queue->rear + 1) % MAX_QUEUE_SIZE == queue->front;
}
void enqueue(Queue *queue, int value) {
if (isQueueFull(queue)) {
printf("Queue is full!\n");
return;
}
queue->data[queue->rear] = value;
queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;
}
int dequeue(Queue *queue) {
if (isQueueEmpty(queue)) {
printf("Queue is empty!\n");
return -1;
}
int value = queue->data[queue->front];
queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;
return value;
}
int main() {
Queue queue;
initQueue(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
printf("Dequeue: %d\n", dequeue(&queue));
printf("Dequeue: %d\n", dequeue(&queue));
printf("Dequeue: %d\n", dequeue(&queue));
return 0;
}
```
在上面的代码中,我们定义了一个 Queue 结构体,其中包含一个数组和两个指针 front 和 rear,分别指向队头和队尾。我们使用 initQueue 函数初始化队列,使用 isQueueEmpty 和 isQueueFull 函数判断队列是否为空或已满,使用 enqueue 和 dequeue 函数分别将元素入队和出队。在主函数中,我们演示了如何使用队列实现排队系统。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)