案例未执行
Case not executing
我有以下代码。问题是,如果我选择一个选项,例如选项 #2 对它正在关闭的程序的元素进行排序,我不明白为什么......我正在使用Eclipse IDE。这些函数执行以下操作:
a( 在列表中插入不允许重复值的数据的函数(如果值允许重复值(不存在,粘贴发生在列表的开头(。有效负载元素为整数。
b( 在列表中插入数据以使列表保持升序排序的函数。有效载荷元素为整数
c( 确定可被 z 值整除的元素数量的函数,接收为参数。 d( 确定大于来自列表的第一个节点。
e( 确定列表中给定值的出现次数的函数。
#include<iostream>
using namespace std;
char menu()
{ char choice;
cout<<"********Choose an option********* n" ;
cout<<"1. duplicate checkn";
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 noden";
cout<<"5. Determine the number of occurrences in a listn";
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;
}
sort(start);
失败,因为start
NULL
这意味着p
NULL
pnode p = start;
您尝试在此处取消引用 NULL 指针
while (p->next != NULL)
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 执行函数时导致崩溃的变量
- 无论条件是否为true,if总是在c++中执行
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 在C++中对T*类型执行std::move的意外行为
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如何在没有信号的情况下从C++执行QML插槽
- 如何确认我的constexpr表达式实际上已经在编译时执行
- C++17中的并行执行策略
- QML按钮点击功能执行顺序
- 程序在执行程序的其余部分之前退出
- 为什么catch中的代码没有被执行
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 案例未执行
- 执行默认值:在C 中的开关案例选择语句中
- 案例语句不执行
- 为什么这个案例块不执行