主要还是为了今后复习和回忆作纪念。
#include "stdio.h"
#include "stdlib.h"
typedef struct queue
{
int *data;
int front;
int rear;
}Queue;
void init(Queue *Q)
{
Q->front=Q->rear=0;
Q->data=(int *)malloc(sizeof(int)*N);
}
bool kong(Queue Q)//判断队列是否为空
{
return Q.front==Q.rear?true:false;
}
bool man(Queue Q)//判断队列是否已满
{
return (Q.rear+1)%N==Q.front?true:false;
}
void enqueue(Queue * Q,int t)//入队
{
if(man(*Q))
{
printf("空间已满!\n");
return;
}
else
{
Q->data[Q->rear]=t;
Q->rear=(Q->rear+1)%N;
}
}
void dequeue(Queue * Q,int *val)//出队
{
if(kong(*Q))
{
printf("队列为空!\n");
return;
}
else
{
*val=Q->data[Q->front];
Q->front=(Q->front+1)%N;
}
}
int lengqueue(Queue Q)//求队列长度
{
return (Q.rear-Q.front+N)%N;
}
void bianli(Queue Q,int *val)//遍历
{
if(kong(Q))
{
printf("队列为空!\n");
return;
}
else
{
int i;
for(i=0;i
{
*(val+i)=Q.data[(Q.front+i)%N];
}
}
}
void main()
{
Queue Q;
int val[N],i,temp;
init(&Q);
enqueue(&Q,8);
enqueue(&Q,0);
enqueue(&Q,2);
enqueue(&Q,3);
bianli(Q,val);
for(i=0;i
{
printf("%d ",val[i]);
}
dequeue(&Q,&temp);
dequeue(&Q,&temp);
putchar('\n');
bianli(Q,val);
for(i=0;i
{
printf("%d ",val[i]);
}
}
在VC6.0中运行为:
-----------------------------
8 0 2 3
2 3 Press any key to continue
-----------------------------