/*
队列:先进先出
队尾:允许插入
队头:删除
*/
#include<stdio.h>
#include<stdlib.h>
typedef int Status;
typedef int QElemType;
typedef struct {
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &Q) {
Q.base=(QElemType*)malloc(100*sizeof(QElemType));
if(!Q.base) exit(0);
Q.front=Q.rear=0;
return 1;
}
Status DestoryQueue(SqQueue &Q) {
if(Q.base)
free(Q.base);
Q.base=NULL;
Q.front=Q.rear=0;
return 1;
}
Status ClearQueue(SqQueue &Q) {
Q.front=Q.rear=0;
return 1;
}
Status QueueLength(SqQueue Q) {
return Q.rear-Q.front;
}
Status EnQueue(SqQueue &Q,QElemType e) {
Q.base[Q.rear]=e;
Q.rear++;
return 1;
}
Status DeQueue(SqQueue &Q,QElemType &e) {
if(Q.front==Q.rear) return 0;
e=Q.base[Q.front];
Q.front=Q.front+1;
return 1;
}
void TraveQueue(SqQueue &Q) {
for(int i=Q.front;i<Q.rear;i++)
{
printf("%3d",Q.base[i]);
}
printf("\n");
}
void main()
{
SqQueue Q;
InitQueue(Q);
QElemType e;
for(int i=0;i<5;i++)
{
EnQueue(Q,i);
}
printf("length=%d\n",QueueLength(Q));
TraveQueue(Q);
DeQueue(Q,e);
printf("length=%d\n",QueueLength(Q));
TraveQueue(Q);
}
数据结构C语言顺序队
最新推荐文章于 2024-08-24 19:20:41 发布