#ifndef SQQUEUE_H
#define SQQUEUE_H
#include"head.h"
#define MAXQSIZE 8
typedef int ElemType;
typedef struct{
ElemType *base;
int head;
int tail;
}SqQueue;
bool InitQueue(SqQueue &Q){
Q.base=(ElemType*)malloc((MAXQSIZE)*sizeof(ElemType));
if(!Q.base) exit(OVERFLOW);
Q.head=Q.tail=0;
return OK;
}
bool ClearQueue(SqQueue &Q){
Q.head=Q.tail=0;
return OK;
}
bool IsEmpty(SqQueue Q){
return Q.head==Q.tail?1:0;
}
bool IsFull(SqQueue Q){
return (Q.tail+1-Q.head)%MAXQSIZE?0:1;
}
int QueueLength(SqQueue Q){
return (Q.tail-Q.head+MAXQSIZE)%MAXQSIZE;
}
bool QueueTraverse(SqQueue Q){
printf("the queue from head to tail is:");
for(int i=Q.head;i!=Q.tail;(++i)%MAXQSIZE){
printf("%d,",Q.base[i]);
}
printf("\n");
return OK;
}
bool EnQueue(SqQueue &Q,ElemType e){
if(IsFull(Q)) return ERROR;
Q.base[Q.tail++]=e;
Q.tail%=MAXQSIZE;
return OK;
}
bool DeQueue(SqQueue &Q,ElemType &e){
if(IsEmpty(Q)) return ERROR;
e=Q.base[Q.head++];
Q.head%=MAXQSIZE;
return OK;
}
bool GetHead(SqQueue &Q,ElemType &e){
if(IsEmpty(Q)) return ERROR;
e=Q.base[Q.head];
return OK;
}
bool GetTail(SqQueue &Q,ElemType &e){
if(IsEmpty(Q)) return ERROR;
e=Q.base[(Q.tail-1+MAXQSIZE)%MAXQSIZE];
return OK;
}
#endif
//SqQueue.h
#ifndef _HEAD
#define _HEAD
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define INFEASIBLE -1
//typedef bool{false,true} bool;
#endif
//head.h