Title: If allowed to insert and delete operations at both ends of the circular queue. Requirements:
① Write the type definition of the circular queue;
② Write the algorithms of "delete from the end of the queue " and "insert from the head of the queue".
①Define the circular queue:
#define MAXSIZE 10;
typedef struct{
Elemtype data[MAXSIZE];
int front;//队头指针
int rear;//队尾指针
}SqQueue;
②Delete from the end of the team:
Status DeQueue(SqQueue &S,int &x){
//&S为出队队列;x存储出队元素的值
if(S.front==S.rear){
//头指针等于尾指针,队空
return ERROR;
}else{
x=S.data[S.rear]; //x取队尾元素的值
S.rear=(S.rear-1+MAXSIZE)%MAXSIZE; //尾指针前移
return OK;
}
}
Insert from the head of the line:
Status EnQueue(SqQueue &S,int &x){
//x为要插入的元素
if(S.rear==(S.front-1+MAXSIZE)%MAXSIZE){
//队满
return ERROR;
}else{
S.data[S.front]=x; //x赋值给头结点的data域
S.front=(S.front-1+MAXSIZE)%MAXSIZE; //头结点前移
return OK;
}
}