C++ 案件不执行
我有以下代码。问题是,如果我选择一个选项,例如选项#2,对程序关闭的元素进行排序,我不明白为什么。。。我正在使用EclipseIDE。这些功能执行以下操作: a) 在不允许重复值(如果值不允许)的列表中插入数据的函数 不存在,粘贴发生在列表的开头)。有效负载元素必须是整数 b) 在列表中插入数据以使列表保持升序排序的函数。有效载荷元件 整数 c) 一种函数,用于确定可被接收到的z值整除的元素数 参数 d) 一种函数,用于确定大于来自 列表的第一个节点C++ 案件不执行,c++,list,graph,C++,List,Graph,我有以下代码。问题是,如果我选择一个选项,例如选项#2,对程序关闭的元素进行排序,我不明白为什么。。。我正在使用EclipseIDE。这些功能执行以下操作: a) 在不允许重复值(如果值不允许)的列表中插入数据的函数 不存在,粘贴发生在列表的开头)。有效负载元素必须是整数 b) 在列表中插入数据以使列表保持升序排序的函数。有效载荷元件 整数 c) 一种函数,用于确定可被接收到的z值整除的元素数 参数 d) 一种函数,用于确定大于来自 列表的第一个节点 #include<iostream&g
#include<iostream>
using namespace std;
char menu()
{ char choice;
cout<<"********Choose an option********* \n" ;
cout<<"1. duplicate check\n";
cout<<"2. Sort the elements \n";
cout<<"3. Determine elements divisible by a value given Z \n";
cout<<"4. Determine the number of elements greater than the first node\n";
cout<<"5. Determine the number of occurrences in a list\n";
cout<<"6. -----Exit-----\n";
cin>>choice;
return choice;
}
struct node
{
int value;
node* prev;
node* next;
};
typedef node* pnode;
int nro=0;
void showlist(pnode start, int div)
{
nro=0;
if(start!=NULL)
{
while(start!=NULL)
{
if(start->value%div==0)
{
nro++;
cout<<start->value<<" ";
}
start=start->next;
}
}
}
void checkvalue(pnode start, int nr)
{
nro=0;
if(start!=NULL)
{
while(start!=NULL)
{
if(start->value==nr)
{
nro++;
}
start=start->next;
}
}
}
bool checkduplicates(pnode start, int val)
{
if(start==NULL) return false;
else
{
while(start!=NULL)
{
if(start->value==val) return true;
start=start->next;
return false;
}
}
}
void sort(pnode start)
{
pnode p=start;
pnode q=NULL;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->value > q->value)
{
int aux;
aux=p->value;
p->value=q->value;
q->value=aux;
}
q=q->next;
}
p=p->next;
}
}
void showbig(pnode start)
{
pnode q=start->next;
while(q!=NULL)
{
if(q->value > start->value)
{
cout<<q->value<<" ";
}
q=q->next;
}
}
int main()
{
pnode start=NULL;
pnode end=NULL;
pnode aux=NULL;
char choice;
int number;
do{
choice=menu();
switch(choice)
{
case '1':
int z;
cout<<"Value: ";
cin>>z;
if(start==NULL)
{
start = new node;
start->value=z;
start->next=NULL;
start->prev=NULL;
end=start;
aux=start;
}
else
{
aux= new node;
aux->value=z;
aux->next=start;
start->prev=aux;
aux->prev=NULL;
start=aux;
}
if (!checkduplicates(start,z))
{cout<<"Duplicate value. Cannot insert.\n";
break;}
break;
case '2':
sort(start);
break;
case '3':
cout<<"Value: ";
cin>>z;
showlist(start,z);
if(nro==0) cout<<"No values found.\n";
else cout<<"\n";
break;
case '4':
showbig(start);
cout<<"\n";
break;
case '5':
cout<<"Value: ";
cin>>z;
checkvalue(start,z);
if(nro==0) cout<<"No values found.\n";
else cout<<nro<<"\n";
break;
default: cout<<"Exit \n";
}
} while (choice !='6');
return 0;
}
e) 确定列表中给定值出现次数的函数
#include<iostream>
using namespace std;
char menu()
{ char choice;
cout<<"********Choose an option********* \n" ;
cout<<"1. duplicate check\n";
cout<<"2. Sort the elements \n";
cout<<"3. Determine elements divisible by a value given Z \n";
cout<<"4. Determine the number of elements greater than the first node\n";
cout<<"5. Determine the number of occurrences in a list\n";
cout<<"6. -----Exit-----\n";
cin>>choice;
return choice;
}
struct node
{
int value;
node* prev;
node* next;
};
typedef node* pnode;
int nro=0;
void showlist(pnode start, int div)
{
nro=0;
if(start!=NULL)
{
while(start!=NULL)
{
if(start->value%div==0)
{
nro++;
cout<<start->value<<" ";
}
start=start->next;
}
}
}
void checkvalue(pnode start, int nr)
{
nro=0;
if(start!=NULL)
{
while(start!=NULL)
{
if(start->value==nr)
{
nro++;
}
start=start->next;
}
}
}
bool checkduplicates(pnode start, int val)
{
if(start==NULL) return false;
else
{
while(start!=NULL)
{
if(start->value==val) return true;
start=start->next;
return false;
}
}
}
void sort(pnode start)
{
pnode p=start;
pnode q=NULL;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->value > q->value)
{
int aux;
aux=p->value;
p->value=q->value;
q->value=aux;
}
q=q->next;
}
p=p->next;
}
}
void showbig(pnode start)
{
pnode q=start->next;
while(q!=NULL)
{
if(q->value > start->value)
{
cout<<q->value<<" ";
}
q=q->next;
}
}
int main()
{
pnode start=NULL;
pnode end=NULL;
pnode aux=NULL;
char choice;
int number;
do{
choice=menu();
switch(choice)
{
case '1':
int z;
cout<<"Value: ";
cin>>z;
if(start==NULL)
{
start = new node;
start->value=z;
start->next=NULL;
start->prev=NULL;
end=start;
aux=start;
}
else
{
aux= new node;
aux->value=z;
aux->next=start;
start->prev=aux;
aux->prev=NULL;
start=aux;
}
if (!checkduplicates(start,z))
{cout<<"Duplicate value. Cannot insert.\n";
break;}
break;
case '2':
sort(start);
break;
case '3':
cout<<"Value: ";
cin>>z;
showlist(start,z);
if(nro==0) cout<<"No values found.\n";
else cout<<"\n";
break;
case '4':
showbig(start);
cout<<"\n";
break;
case '5':
cout<<"Value: ";
cin>>z;
checkvalue(start,z);
if(nro==0) cout<<"No values found.\n";
else cout<<nro<<"\n";
break;
default: cout<<"Exit \n";
}
} while (choice !='6');
return 0;
}
#包括
使用名称空间std;
字符菜单()
{字符选择;
coutvalue=aux;
}
q=q->next;
}
p=p->next;
}
}
无效显示大(pnode开始)
{
pnode q=开始->下一步;
while(q!=NULL)
{
如果(q->值>开始->值)
{
coutnext=NULL;
开始->上一个=空;
结束=开始;
aux=启动;
}
其他的
{
aux=新节点;
辅助->值=z;
辅助->下一步=开始;
开始->上一次=辅助;
aux->prev=NULL;
启动=辅助;
}
如果(!检查重复项(开始,z))
{cout
失败,因为start
为NULL
,这意味着p
为NULL
pnode p = start;
您尝试在这里取消对空指针的引用
while (p->next != NULL)
失败,因为start
为NULL
,这意味着p
为NULL
pnode p = start;
您尝试在这里取消对空指针的引用
while (p->next != NULL)
对于第2种情况,如果start
为空,则会出现segfault和程序崩溃。对于第2种情况,如果start
为空,则会出现segfault和程序崩溃。在我看来,您必须多次使用选项1来构建其他选项的列表。在我看来,您必须使用选项1多次为其他要处理的选项建立列表?