#include<stdio.h>
#define m 10
#define OK 1
#define ERROR 0
//全局变量
typedef int intQueue;
int count = 0;//计数
typedef struct {
intQueue Q[m];
int front, rear, tag;
}Queue;
intQueue startup( Queue &q) {//初始化
q.front = 0;
q.rear = 0;
q.tag = 0;
return OK;
}
intQueue enqueue(Queue& q,int n) {//进队列 n 为进队列的个数
for (int i = 1; i <= n; i++) {//从1 到 n 依次进队列
if (count != m) {
q.Q[q.rear] = i;//给第一个Q[0]赋值1
q.rear = (q.rear + 1) % m;//依次向下一个走,%m是为了不会取到负数
count++;
}
else q.tag=1;//当count==m的时候 就用标志tag
if (q.tag == 1 && q.front == q.rear)//即队列已满
{
printf("队列为满\n");
return ERROR;
}
printf("进队列的数是:%d,", i);
printf("队列的长度为:%d,\n", count);
}
return OK;
}
intQueue dequeue(Queue& q, intQueue &e,int n) {//出队列
for (int i = 1; i <= n; i++) {//先进队列的先出
if (count != 0) {
e = q.Q[q.front];//将第一个进队列的元素给e,然后指向下一个继续出队列直到为空
q.front = (q.front + 1) % m;
count--;
}
else q.tag = 0;
if (q.tag == 0 && q.front == q.rear) {
printf("队列为空\n");
return ERROR;
}
printf("出队列的数是:%d,", e);
printf("队列的长度为:%d,\n", count);
}
return OK;
}
int main() {
Queue Q;
int num=0;
startup(Q);
enqueue(Q,12);
printf("\n");
dequeue(Q,num,12);
}
实现图: