请教一个关于归纳的问题
假设以数组sequ[m]存放循环队列的元素,同时设变量rear和quelen分别指示循环队列中队尾元素的位置和内含元素的个数。试给出此循环队列的队满条件,并写出相应的入队列和出队列的算法(在出队列算法中要返回队头元素)。#include "iostream"
#include "stdlib.h"
#include "stdio.h"
#include "malloc.h"
#define OK 1
#define m 5
using namespace std;
typedef struct{
int sequ[m];
int length;
int rear;
}SqQueue;
SqQueue Q;
int n;
int InitQueue(SqQueue &Q){
Q.rear=0;
Q.length=0;
return OK;
}
int EnQueue(SqQueue &Q,int e){
if(Q.length>=m){
cout<<"error"<<endl;
exit(0);
}
Q.sequ[Q.rear]=e;
Q.rear=(Q.rear+1)%m;
Q.length++;
return OK;
}
int DeQueue(SqQueue &Q,int &e){
if(Q.length<=0){
cout<<"error"<<endl;
exit(0);
}
e=Q.sequ[(m+Q.rear-Q.length)%m];//这条件怎么想出来的啊,我想了半天都想不到
Q.length--;
return OK;
}
int Put(SqQueue Q){
int i;
for(i=(m+Q.rear-Q.length)%m;i<(m+Q.rear-Q.length)%m+Q.length;i++) cout<<Q.sequ[i]<<" ";
cout<<endl;
return OK;
}
void main(){
int a,e,i;
InitQueue(Q);
cout<<"请输入加入元素的个数:"<<endl;
cin>>a;
cout<<"请输入这些元素的值:"<<endl;
for(i=0;i<a;i++){
cin>>e;
EnQueue(Q,e);
}
cout<<"现在的队列元素为:"<<endl;
Put(Q);
cout<<"请输入要删除的元素个数:"<<endl;
cin>>a;
cout<<"你现在删除的是:";
for(i=0;i<a;i++){
DeQueue(Q,e);
cout<<e<<" ";
}
cout<<"现在的队列元素为:"<<endl;
Put(Q);
}