先放代码
点击查看代码
#include <stdio.h>
#define Maxsize 50
#define true 1
#define false 0
typedef int Elemtype;
typedef struct{
Elemtype data[Maxsize];
int front,rear; //front指向第一个元素(队头),rear指向队尾的后面
}SqQueue;
//初始化
void InitQueue(SqQueue *Q){
Q->rear = Q->front = 0; //可以拿来判空
}
//判队空
int isEmpty(SqQueue Q){
if(Q.rear == Q.front)
return true;
else
return false;
}
//入队
//队不满时,先将值送到队尾元素,再将队尾指针+1
int EnQueue(SqQueue *Q,Elemtype x){
if((Q->rear + 1) % Maxsize == Q->front) //队满
return false;
Q->data[Q->rear]=x;
Q->rear=(Q->rear+1)%Maxsize;
return true;
}
//出队
//队不空时,先取队头元素,再将队头指针+1
int DeQueue(SqQueue *Q){
int x;
if(Q->rear == Q->front)
return false;
x = Q->data[Q->front];
Q->front = (Q->front+1) % Maxsize;
return x;
}
void Q_p(SqQueue Q){
int q = Q.front;
while(Q.front!=Q.rear)
printf("%d ",Q.data[Q.front++]);
printf("\n");
Q.front = q;
}
int main() {
SqQueue Q;
InitQueue(&Q);
EnQueue(&Q,0);EnQueue(&Q,1);EnQueue(&Q,2);Q_p(Q);
DeQueue(&Q);Q_p(Q);
return 0;
}