求助一道题目…老师讲的完全听不懂,想了好久了 QAQ
实验1.1 环形队列一、 实验目的
掌握环形队列顺序结构的概念,理解环形队列的基本运算。
二、 实验内容
1、 删除环形队列第k个元素
编写算法的要求:从键盘输入被删除元素的位置k,由x返回该元素值,并输出队列中元素的个数。
请将上述编好的函数插入相应的实验框架程序,并调试运行,分析实验结果。
三、 环形队列实验程序
#include<iostream.h>
#define max 15
const int m = max+1;
struct cqueue{
int e[max];
int front,rear;
};
void setnull(cqueue &cq) //置环形队列cq为空
{
cq.front = 0;
cq.rear = 0;
}
void create(cqueue &cq) //输入环形队列cq元素的值
{
int d;
cout<<"INPUT DATA(END -1):\n\t";
cout<<"DATA:";
cin>>d;
while (d != -1){
cq.e[cq.rear] = d;
cq.rear = (cq.rear + 1) % m;
cin>>d;
}
}
void outcq(cqueue cq) //输出环形队列cq中元素的值
{
int i;
i = cq.front;
while (i != cq.rear ){
cout<<cq.e[i]<<'\t';
i = (i + 1) % m;
}
cout<<'\n';
}
void error(char err[]) //输出出错信息
{
cout<<err<<"出错\n";
}
void insert_cqueue(cqueue &cq,int x)
//入队算法
{
int p;
p = cq.rear;
cq.rear = (cq.rear + 1) % m;
if (cq.front == cq.rear){
cq.rear = p;
error("err1");//环形队列满出错
}
else cq.e[p] = x;
}
void delete_cqueue(cqueue &cq,int &x)
//出队算法
{
if (cq.front == cq.rear)
error("err2");//环形队列空出错
else {
x = cq.e[cq.front];
cq.front = (cq.front + 1) % m;
}
}
int length(cqueue cq)
//求环形队列cq长度函数
{
int len;
len = (m + cq.rear - cq.front) % m;
return len;
}
int empty(cqueue cq)
//判环形队列cq是否为空函数
{
int emp;
emp = cq.rear == cq.front;
return emp;
}
void delelemk(cqueue &cq,int k,int &x)
//算法功能删除队列cq第i个元素,其值由x返回
{
}
void main(void)
{
cqueue cq;
char c;
int k,x;
cout<<" * * * THE PROGRAM FOR CQUEUE * * *\n";
setnull(cq);
create(cq);
do{
cout<<"TO SELECT COMMAND(I,D,O,K,E)\n\t";
cout<<"COMMAND:";cin>>c;
switch(c){
case 'I':cout<<"TO INSERT NEW ELEMENT IN THE CQUEUE cq\n";
cout<<"VALUE OF NEW ELEMENT(x) = ";
cin>>x;
insert_cqueue(cq,x);
outcq(cq);
break;
case 'D':cout<<"TO DELETE ELEMENT IN THE CQUEUE cq\n";
delete_cqueue(cq,x);
outcq(cq);
break;
case 'O':cout<<"ELEMENT OF THE CQUEUE cq READS AS FOLLOWS:\n";
outcq(cq);
break;
case 'K':cout<<"TO DELETE ELEMENT ON NUMBER SEQUENCE k IN THE CQUEUE cq\n";
cout<<"NUMBER SEQUENCE OF THE ELEMENT(k) = ";
cin>>k;
delelemk(cq,k,x);
outcq(cq);
break;
case 'E':break;
default:error("命令字");
}
}
while(c != 'E');
}