//循环队列
#include <cstdio>
#define MaxSize 5
typedef struct{
int data[MaxSize];
int front,rear;
int size;
}SqQueue;
void InitQueue(SqQueue &Q){
Q.front = Q.rear =0;
Q.size = 0;
}
bool isEmpty(SqQueue Q){
return !Q.size;
}
bool EnQueue(SqQueue &Q,int x){
if(Q.size == MaxSize){
printf("插入失败,队列已满!\n");
return false;
}
Q.data[Q.rear] = x;
Q.rear = (Q.rear+1)%MaxSize;
Q.size+=1;
printf("插入成功!\n");
return true;
}
bool DeQueue(SqQueue &Q,int &x){
if(isEmpty(Q)){
printf("删除失败,该队列为空!\n");
return false;
}
x = Q.data[Q.front];
Q.front = (Q.front+1)%MaxSize;
Q.size-=1;
printf("删除成功!\n");
return true;
}
bool GetQueue(SqQueue Q,int &x){
if(isEmpty(Q)){
printf("读取队头失败,该队列为空!\n");
return false;
}
x = Q.data[Q.front];
printf("队头元素为%d\n",x);
return false;
}
int main(){
int x;
SqQueue Q;
InitQueue(Q);
if(isEmpty(Q)){
printf("队列为空!\n");
}
EnQueue(Q,1);
EnQueue(Q,2);
GetQueue(Q,x);
EnQueue(Q,3);
EnQueue(Q,4);
EnQueue(Q,5);
EnQueue(Q,6);
DeQueue(Q,x);
DeQueue(Q,x);
GetQueue(Q,x);
DeQueue(Q,x);
DeQueue(Q,x);
DeQueue(Q,x);
DeQueue(Q,x);
DeQueue(Q,x);
return 0;
}
循环队列(C++)
最新推荐文章于 2024-08-30 16:00:44 发布