进出队列
程序员文章站
2022-06-08 08:11:06
...
进出队列代码如下
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define FALSE 0
#define ERROR 0
#define TRUE 1
#define M 100
typedef struct {
int elem[M];
int rear;
int front;
} SqQueue;
typedef int Status;
//初始化队列
Status SqQueueInit(SqQueue *S){
S->front = S->rear = 0;
return OK;
}
//入队函数
int SqQueueInsert(SqQueue *S, int x)
{
if ((S->rear + 1) % M == S->front)
{
return FALSE;
}
S->elem[S->rear] = x;
S->rear = (S->rear + 1) % M;
return TRUE;
}
//出队函数
int SqQueueDelete(SqQueue *S, int *x)
{
if (S->rear == S->front)
{
return FALSE;
}
*x=S->elem[S->front] ;
S->front = (S->front + 1) % M;
return TRUE;
}
int main() {
SqQueue S;
int j;
int m;
SqQueueInit(&S); //初始化函数
SqQueueInsert(&S,55); //入队函数
SqQueueInsert(&S,32);
SqQueueInsert(&S,16);
SqQueueInsert(&S,51);
SqQueueInsert(&S,34);
for (j = 0; j < M; ++j) {
if(SqQueueDelete(&S,&m)==OK) //出队函数
printf("%d ",m);
}
return 0;
}
运行结果