#include <stdio.h>
#include <stdlib.h>
#define N 6
typedef struct
{
int data[N];
int tail;
int head;
}LoopQueue;
LoopQueue* initQueue()
{
LoopQueue* p = (LoopQueue*)malloc(sizeof(LoopQueue));
if (p == NULL)
{
perror("init malloc err\n");
return NULL;
}
p->head = 0;
p->tail = 0;
return p;
}
int isNullQueue(LoopQueue* p)
{
return p->head == p->tail;
}
int isFullQueue(LoopQueue* p)
{
return (p->tail +1) % N == 0;
}
void inQueue(LoopQueue* p, int data)
{
if (isFullQueue(p))
{
printf("队列满!\n");
return;
}
p->data[p->tail] = data;
p->tail = (p->tail +1) % N;
}
void outQueue(LoopQueue* p)
{
if (isNullQueue(p))
{
printf("空队!\n");
return;
}
int temp = p->data[p->head];
p->head = (p->head + 1) % N;
printf("出列:%d\n", temp);
}
void noneQueue(LoopQueue* p)
{
while(!isNullQueue(p))
{
outQueue(p);
}
}
void queueLength(LoopQueue* p)
{
if (p->head < p->tail)
{
printf("队长:%d\n", p->tail - p->head);
}
else
{
printf("队长:%d\n", p->tail - p->head + N);
}
}
void printQueue(LoopQueue* p)
{
for (int i = p->head; i < p->tail; i++)
{
printf("%d-", p->data[i]);
}
printf("NULL\n");
}
int main(int argc, char const *argv[])
{
LoopQueue* p = initQueue();
inQueue(p, 1);
inQueue(p, 2);
inQueue(p, 3);
inQueue(p, 4);
inQueue(p, 5);
queueLength(p);
printQueue(p);
outQueue(p);
queueLength(p);
printQueue(p);
noneQueue(p);
printQueue(p);
return 0;
}
