//队列演示
#include<stdio.h>
#define QUEUE_LENGTH 100
//仅能处理非负整数
struct Queue
{
int element[QUEUE_LENGTH];
int head;
int tail;
};
int QueueIn(struct Queue* p,int num)
{
//满了
if(p->tail>QUEUE_LENGTH-1)
{
return -1;
}
p->element[p->tail]=num;
p->tail++;
return 1;
}
int QueueOut(struct Queue* p)
{
if(p->head==p->tail)
return -1;
return p->element[p->head++];
}
void PrintQueue(struct Queue* p)
{
int position=p->head;
while(position!=p->tail)
{
printf("%d ",p->element[position]);
position++;
}
printf("\n");
}
int main()
{
//初始化
struct Queue queue;
queue.head=0;
queue.tail=0;
QueueIn(&queue,1);
QueueIn(&queue,2);
PrintQueue(&queue);
QueueOut(&queue);
PrintQueue(&queue);
QueueOut(&queue);
PrintQueue(&queue);
QueueOut(&queue);
PrintQueue(&queue);
QueueIn(&queue,3);
PrintQueue(&queue);
return 1;
}