请问图片那里引发了异常应该怎么处理
遇到取消对nullptr 指针的引用是哪里出了问题
#include
#include
using namespace std;
typedef struct { //结构体定义
int num;
char name[10];
}student;
typedef struct Lnode{
student stu;
Lnode* pre;
Lnode* next;
}*lnode;
bool initlnode(lnode& l) {//链表初始化
lnode p= new Lnode;
p = nullptr;
return true;
}
bool creatlnode(lnode& l, const int a) { //链表赋值
lnode current = l;
cout << "请输入链表信息" << endl;
for (int i = 1; i <= a; i++) {
lnode temp=new Lnode;
cout << "请输入第" << i << "个学生的数据" << endl;
cin >> temp->stu.num;
cin >>temp->stu.name;
current->next = temp;
temp->next = nullptr;
temp->pre = current;
current = temp;
}
return true;
}
bool insert(lnode& l, const student a,const int b) {//插入到b后
if (l = nullptr) return false;
lnode temp=new Lnode;
temp->stu= a;
lnode current = l;
int i = 0;
for( i=0;inext!=nullptr;i++){
current = current->next;//current指向b
}
if (i < b) return false;
temp->next =nullptr;
temp->pre = current;
current->next = temp;
return true;
}
bool eraselnode(lnode& l, const int a) {//删除第a个元素
if (l == nullptr) {
cout << "空链表,无法删除" << endl;
return false;
}
int i = 0;
lnode current = l;
for (i; i < a&¤t!=nullptr; i++) {
current = current->next;
}
if (i < a || a < 1) {
cout << "删除位置错误" << endl;
return false;
}
lnode p = current;
current->pre->next = current->next;
current->next->pre = current->pre;
delete p;
return true;
}
bool searchlnode(lnode& l, const int a,student* b) {//查找a处的元素
if (l == nullptr) {
cout << "空链表,无法查找" << endl;
return false;
}
int i = 0;
lnode current = l;
for (i; i < a && current != nullptr; i++) {
current = current->next;
}
if (i < a || a < 1) {
cout << "查找位置错误" << endl;
return false;
}
b = &(current->stu);
return true;
}
bool findbeforeAndAfter(lnode& l, const int i, lnode m,const int j,lnode n,int num) {//查找第i个前驱,第j个后继
lnode current = l;
while (current->stu.num != num&¤t->next!=nullptr) {
current = current->next;
}
if (current->stu.num != num && current->next == nullptr) {
return false;
}
lnode temp = current;
for (int t = i; t > 0; t--) {
temp = temp->pre;
if (temp->pre = nullptr) {
m = nullptr;
cout << "前驱查找失败,请检查您输入的数值是否在正确范围内" << endl;
return false;
}
}
m = temp;
lnode temp2 = current;
for (int t = j; t > 0; t--) {
temp2 = temp2->next;
if (temp2->next = nullptr) {
n= nullptr;
cout << "后继查找失败,请检查您输入的数值是否在正确范围内" << endl;
return false;
}
}
n = temp2;
return true;
}
void print(lnode& l) {
lnode current = l;
while (current) {
cout << current->stu.num << " " << current->stu.name;
current = current->next;
}
}
int main() {
lnode stulist;
if (initlnode(stulist)) {
cout << " 请输入学生个数" << endl;
int stnum;
cin >> stnum;
creatlnode(stulist, stnum);
}
else cout << "链表初始化错误,请检查程序" << endl;
cout << "请选择您要实现的功能:" << endl;
cout << "1.插入学生信息 2.删除学生信息 3.查找学生信息 4.查找前驱后继" << endl;
int a;
cin >> a;
student* searchout = nullptr;
switch (a) {
case 1:
student stinsert;
cout << "请输入插入学生的学号" << endl;
cin >> stinsert.num;
cout << "请输入插入学生的姓名" << endl;
cin >> stinsert.name;
cout << "请输入要插入的位置(将在你输入的元素位置之后插入)" << endl;
int locinsert;
cin >> locinsert;
if (insert(stulist, stinsert, locinsert)) {
cout << "插入成功" << endl;
print(stulist);
}
else cout << "插入失败" << endl;
break;
case 2:
cout << "请输入你要删除学生的位置" << endl;
int locerase;
cin >> locerase;
if (eraselnode(stulist, locerase)) {
cout << "删除成功" << endl;
print(stulist);
}
break;
case 3:
cout << "请输入你要查找学生的位置" << endl;
int locsearch;
cin >> locsearch;
if (searchlnode(stulist, locsearch, searchout)) {
cout << "查找成功,您查找的学生信息为:" << endl;
cout << searchout->num<< " " << searchout->name;
}
break;
case 4:
cout << "请输入学生学号" << endl;
int flagstu;
cin >> flagstu;
cout << "请输入您向前查找的数目" << endl;
int i;
cin >> i;
cout << "请输入您向后查找的数目" << endl;
int j;
cin >> j;
lnode p=nullptr, q=nullptr;
if (findbeforeAndAfter(stulist, i, p, j, q, flagstu)) {
cout << "查找成功" << endl;
cout << "第" << i << "个前驱为:" << endl;
cout << p->stu.num << " " << p->stu.name;
cout << "第" << j << "个后继为:" << endl;
cout << q->stu.num << " " << q->stu.name;
break;
}
}
return 0;
}
你检查一下传入的l变量是不是空的啊,或者没有初始化
bool initlnode(lnode& l) {//链表初始化
lnode p= new Lnode;
p = nullptr;
return true;
}
你这初始化函数和传入的变量毫无关系啊,根本没有创建啊,改为:
bool initlnode(lnode& l) {//链表初始化
l = new Lnode;
if(l==NULL)
return false;
l->next = NULL;
return true;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!