<span style="font-size:24px;">#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define OK 1
#define ERROR -1
#define MAXSIZE 10
#define OVERFLOW -2
typedef int statue;
typedef int QElemType;
typedef struct{
QElemType *pBase;
int front;
int rear;
}SqQueue;
statue iniSQ(SqQueue *sq){//初始化
sq->pBase=(QElemType *)malloc(sizeof(QElemType)*MAXSIZE);
if(sq->pBase==NULL){
printf("内存分配失败\n");
exit(OVERFLOW);
}
sq->front=sq->rear=0;
return OK;
}
bool isEmpty(SqQueue sq){
if(sq.front==sq.rear)
return true;
return false;
}
bool isFail(SqQueue sq){
if((sq.rear+1)%MAXSIZE==sq.front)
return true;
return false;
}
int getLength(SqQueue sq){
return (sq.rear-sq.front+MAXSIZE)%MAXSIZE;
}
statue EnQueue(SqQueue *sq,QElemType val){//入队
if(isFail(*sq))
return ERROR;
sq->pBase[sq->rear]=val;
sq->rear=(sq->rear+1)%MAXSIZE;
return OK;
}
statue DeQueue(SqQueue *sq,QElemType *val){//出队
if(isEmpty(*sq))
return ERROR;
*val=sq->pBase[sq->front];
sq->front=(sq->front+1)%MAXSIZE;
return OK;
}
statue getHead(SqQueue sq,QElemType *val){
if(isEmpty(sq))
return ERROR;
*val=sq->pBase[sq->front];
return OK;
}
int main(){
SqQueue sq;
iniSQ(&sq);
EnQueue(&sq,2);
EnQueue(&sq,3);
int val;
DeQueue(&sq,&val);
printf("%d\n",val);
printf("%d",getLength(sq));
return 0;
}</span>
顺序队列
最新推荐文章于 2018-06-02 16:11:00 发布