C++ C++;如何修复引发的异常:写入访问冲突。这是nullptr。第20行
*创建一个简单的链表程序来创建包含C++ C++;如何修复引发的异常:写入访问冲突。这是nullptr。第20行,c++,exception,C++,Exception,*创建一个简单的链表程序来创建包含 class node { void *info; node *next; public: node (void *v) {info = v; next = 0; } void put_next (node *n) {next = n;} node *get_next ( ) {return next;} void *get_info
class node {
void *info;
node *next;
public:
node (void *v) {info = v; next = 0; }
void put_next (node *n) {next = n;}
node *get_next ( ) {return next;}
void *get_info ( ) {return info;}
};
能够初步填写清单。提供插入/追加节点和从链表中删除节点的功能。能够显示列表的内容。
在插入/追加、删除和显示数据时,编写一个至少传入5个值的小驱动程序(以便创建5个节点),显示程序操作。
输出:显示数据时,请确保使用信息性标签–使用术语“插入”、“附加”、“删除”和显示操作的任何其他术语。所有显示的数据必须以导师能够轻松阅读和理解的方式显示*
我的代码运行时抛出异常:写入访问冲突。这是nullptr。当我调试它时,在第20行。在输出中,它也在我的列表的删除04处停止,并且在第156行之后不再输出进一步的数据。我不知道如何解决这个问题。我试图用另一个cout语句强制它输出新的列表,但没有成功。任何协助都将不胜感激。下面是完整的代码
#include <iostream>
#include <iomanip>
using namespace std;
class Node
{
void* info;
Node* next;
public:
Node(void* v) { info = v; next = 0; }
void put_next(Node* n)
{
next = n;
}
Node* get_next()
{
return next;
}
void* get_info()
{
return info;
}
};
class List {
Node* head;
public:
List() { head = NULL; };
void PRINT();
void APPEND(void* info);
void DELETE(void* info);
};
void List::PRINT() {
Node* temp = head;
if (temp == NULL)
{
cout << "Enter Values" << endl;
return;
}
if (temp->get_next() == NULL)
{
cout << *(int*)temp->get_info();
cout << " => ";
cout << "Invalid" << endl;
}
else
{
while (temp != NULL)
{
cout << *(int*)temp->get_info();
cout << " --> ";
temp = temp->get_next();
}
cout << "Invalid" << endl;
}
}
void List::APPEND(void* info) {
Node* newNode = new Node(info);
newNode->put_next(NULL);
Node* temp = head;
if (temp != NULL)
{
while (temp->get_next() != NULL)
{
temp = temp->get_next();
}
temp->put_next(newNode);
}
else
{
head = newNode;
}
}
void List::DELETE(void* info) {
Node* temp = head;
if (temp == NULL)
{
cout << "Enter Values" << endl;
return;
}
if (temp->get_next() == NULL)
{
if ((int*)(temp->get_info()) == info)
{
delete temp;
head = NULL;
}
}
else
{
Node* previous = NULL;
while (temp != NULL)
{
if ((int*)(temp->get_info()) == info) break;
previous = temp;
temp = temp->get_next();
}
previous->put_next(temp->get_next());
delete temp;
}
}
int main()
{
List list;
int a = 04;
int b = 11;
int c = 12;
int d = 15;
int e = 29;
int* Node1 = &a;
int* Node2 = &b;
int* Node3 = &c;
int* Node4 = &d;
int* Node5 = &e;
cout << "Append: 04" << endl;
list.APPEND(Node1);
cout << "Append: 11" << endl;
list.APPEND(Node2);
cout << "Append: 12" << endl;
list.APPEND(Node3);
cout << "Append: 15" << endl;
list.APPEND(Node4);
cout << "Append: 29" << endl;
list.APPEND(Node5);
cout << endl << "Print List" << endl;
list.PRINT();
cout << endl << "Delete 04" << endl;
list.DELETE(Node1);
list.PRINT();
cout << "Start New List" << endl;
cout << endl << "Delete 12" << endl;
list.DELETE(Node3);
list.PRINT();
return 0;
}
#包括
#包括
使用名称空间std;
类节点
{
作废*信息;
节点*下一步;
公众:
节点(void*v){info=v;next=0;}
void put_next(节点*n)
{
next=n;
}
节点*get_next()
{
下一步返回;
}
void*get_info()
{
退货信息;
}
};
班级名单{
节点*头;
公众:
List(){head=NULL;};
作废打印();
作废追加(作废*信息);
作废删除(作废*信息);
};
作废列表::打印(){
节点*温度=头部;
if(temp==NULL)
{
无法将_放入下一个(新节点);
}
其他的
{
头=新节点;
}
}
作废列表::删除(作废*信息){
节点*温度=头部;
if(temp==NULL)
{
无法获取_info())==info)
{
删除临时文件;
head=NULL;
}
}
其他的
{
Node*previous=NULL;
while(temp!=NULL)
{
如果((int*)(temp->get_info())==info)中断;
先前=温度;
temp=temp->get_next();
}
上一步->下一步放置(临时->下一步获取());
删除临时文件;
}
}
int main()
{
名单;
INTA=04;
int b=11;
int c=12;
int d=15;
int e=29;
int*Node1=&a;
int*Node2=&b;
int*Node3=&c;
int*Node4=&d;
int*Node5=&e;
cout因此,无论何时删除第一个
节点,您都没有将头部
设置为头部的下一个节点
在DELETE
函数的else
语句中修改的部分处注释
代码:
else
{
Node*previous=NULL;
while(temp!=NULL)
{
如果((int*)(temp->get_info())==info){
//如果节点是头部,则更改头部
如果(温度==水头)
head=temp->get_next();
打破
}
先前=温度;
temp=temp->get_next();
}
//如果节点是头部,则跳过此操作
如果(临时->获取下一步()!=头部)
上一步->下一步放置(临时->下一步获取());
删除临时文件;
}
最好的方法是熟悉调试器…当您删除第一个节点时,DELETE
函数无法正确重置head
。如果您能告诉我如何使用调试器,我将不胜感激。我正在使用Visual Studio 2019。(C++控制台应用程序)这是我第一次遇到这个问题。我怎么才能正确地重置头部?我不使用VS自己,但是确实有好的教程。而且,你的代码看起来很奇怪,不是链接列表的惯用方法。看看一些实现并理解它,并尝试在C++中模仿。BTW,而不是THA。使用<代码> Value*/Cord>,使用<代码>模板<代码>。数据类型是传递给模板的数据参数。@ CuMIST187,如果这回答了您的问题,请考虑接受它。
//Node.h
#pragma once
class Node
{
void* info;
Node* next;
public:
Node(void* v) { info = v; next = 0; }
void put_next(Node* n)
{
next = n;
}
Node* get_next()
{
return next;
}
void* get_info()
{
return info;
}
};
//List.h
#pragma once
#include "Node.h"
#include <iomanip>
class List {
Node* head;
public:
List() { head = NULL; };
void PRINT();
void APPEND(void* info);
void DELETE(void* info);
};
//List.cpp
#include <iostream>
#include "List.h"
using namespace std;
void List::PRINT() {
Node* temp = head;
if (temp == NULL)
{
cout << "Enter Values" << endl;
return;
}
if (temp->get_next() == NULL)
{
cout << *(int*)temp->get_info();
cout << " => ";
cout << "Invalid" << endl;
}
else
{
while (temp != NULL)
{
cout << *(int*)temp->get_info();
cout << " --> ";
temp = temp->get_next();
}
cout << "Invalid" << endl;
}
}
void List::APPEND(void* info) {
Node* newNode = new Node(info);
newNode->put_next(NULL);
Node* temp = head;
if (temp != NULL)
{
while (temp->get_next() != NULL)
{
temp = temp->get_next();
}
temp->put_next(newNode);
}
else
{
head = newNode;
}
}
void List::DELETE(void* info) {
Node* temp = head;
if (temp == NULL)
{
cout << "Enter Values" << endl;
return;
}
if (temp->get_next() == NULL)
{
if ((int*)(temp->get_info()) == info)
{
delete temp;
head = NULL;
}
}
else
{
Node* previous = NULL;
while (temp != NULL)
{
if ((int*)(temp->get_info()) == info) {
// if node is head then changing the head
if (temp == head)
head = temp->get_next();
break;
}
previous = temp;
temp = temp->get_next();
}
// skip this operation if node is head
if (temp->get_next() != head)
previous->put_next(temp->get_next());
delete temp;
}
}
//Main.cpp
#include <iostream>
#include "List.h"
using namespace std;
int main()
{
List list;
int a = 04;
int b = 11;
int c = 12;
int d = 15;
int e = 29;
int* Node1 = &a;
int* Node2 = &b;
int* Node3 = &c;
int* Node4 = &d;
int* Node5 = &e;
cout << "Append: 04" << endl;
list.APPEND(Node1);
cout << "Append: 11" << endl;
list.APPEND(Node2);
cout << "Append: 12" << endl;
list.APPEND(Node3);
cout << "Append: 15" << endl;
list.APPEND(Node4);
cout << "Append: 29" << endl;
list.APPEND(Node5);
cout << endl << "Print List" << endl;
list.PRINT();
cout << endl << "Delete 04" << endl;
list.DELETE(Node1);
list.PRINT();
cout << "Start New List" << endl;
cout << endl << "Delete 12" << endl;
list.DELETE(Node3);
list.PRINT();
return 0;
}