순환 대기 열 - 순차 저장 구조 - 데이터 구조
1879 단어 데이터 구조
간단 한 기본 조작, 입 대, 출 대, 맞 춤 형 요 소 를 취하 여 빈 대기 열, 대기 열 길 이 를 판단 합 니 다.
예 코드:
#include
#include
#include
#define ERROR 0
#define OK 1
#define OVERFLOW -1
#define MAXSIZE 10
typedef int Elemtype;
typedef int Status;
typedef struct SqQue
{
Elemtype *base;
int front;
int rear;
}SqQue;
Status InitQue(SqQue &q) //initialize the queue
{
q.base=(Elemtype*)malloc(MAXSIZE*sizeof(Elemtype));
if(!q.base) exit(OVERFLOW);
q.front=q.rear=0;
return OK;
}
bool EmptyQue(SqQue q) //if empty
{
if(q.front==q.rear) return true;
else return false;
}
Status EnQue(SqQue &q,Elemtype e)
{
if((q.rear+1)%MAXSIZE==q.front) return ERROR; //queue full
q.base[q.rear]=e;
q.rear=(q.rear+1)%MAXSIZE;
return OK;
}
Status DisplayQue(SqQue q)
{
int i;
if(EmptyQue(q)) printf("The queue is empty!
");
else
{
i=q.front;
while(i!=q.rear)
{
printf("%d ",q.base[i]);
i=(i+1)%MAXSIZE;
}
printf("
");
return OK;
}
}
int QueLength(SqQue q)
{
return (q.rear-q.front+MAXSIZE)%MAXSIZE;
}
Status DeQue(SqQue &q)
{
Elemtype e;
if(EmptyQue(q)) return ERROR;
e=q.base[q.front];
q.front=(q.front+1)%MAXSIZE;
return OK;
}
Status GetQueHead(SqQue q,Elemtype &e)
{
if(EmptyQue(q)) return ERROR; //queue empty
e=q.base[q.front];
return OK;
}
int main()
{
SqQue que;
int n,i,len;
Elemtype e;
InitQue(que);
if(EmptyQue(que)) printf("The initial queue is empty.
");
printf("Input the number of queue n(don't greater than MAXSIZE):");
scanf("%d",&n);
for(i=0;i