C语言代码
时间: 2023-07-09 17:21:25 浏览: 43
好的,下面是一个简单的D士兵队列的C语言代码,供你参考:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int main()
{
int queue[MAX_SIZE];
int front = -1, rear = -1;
// 入队操作
void enqueue(int x)
{
if (rear == MAX_SIZE - 1)
{
printf("队列已满,无法插入!\n");
return;
}
else
{
queue[++rear] = x;
}
}
// 出队操作
int dequeue()
{
if (front == rear)
{
printf("队列已空,无法删除!\n");
return -1;
}
else
{
return queue[++front];
}
}
// 展示队列中的元素
void display()
{
printf("队列中的元素为:\n");
for (int i = front + 1; i <= rear; i++)
{
printf("%d ", queue[i]);
}
printf("\n");
}
// 测试队列操作
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
display();
dequeue();
display();
enqueue(5);
enqueue(6);
display();
return 0;
}
```
这个代码使用了数组来实现队列的功能,其中使用了三个基本操作:入队、出队和展示队列中的元素。你可以根据自己的需要进行修改和优化。