C++;链表运行时错误:未处理的异常-写入位置冲突 我试图在C++中构建我自己的链表实现。我的代码正在编译,但显然我的指针引用无效内存地址时存在一些问题

C++;链表运行时错误:未处理的异常-写入位置冲突 我试图在C++中构建我自己的链表实现。我的代码正在编译,但显然我的指针引用无效内存地址时存在一些问题,c++,pointers,linked-list,C++,Pointers,Linked List,以下是我的实现: #include <iostream> #include <string> using namespace std; class Node { private: string _car; Node* nextNode; public: void setCar(string car) { _car = car; }

以下是我的实现:

#include <iostream>
#include <string>

using namespace std;

class Node
{
    private:
        string _car;
        Node* nextNode;

    public:
        void setCar(string car)
        {
            _car = car;
        }

        string getCar()
        {
            return _car;
        }

        void setNextNode(Node* node)
        {
            nextNode = node;
        }

        Node* getNextNode()
        {
            return nextNode;
        }
};

Node* findLast(Node* node)
{
    Node* nodeOut = NULL;
    while (node->getNextNode() != NULL)
    {
        nodeOut = node->getNextNode();
    }
    return nodeOut;
}

string toString(Node* node)
{
    string output = "";
    while (node->getNextNode() != NULL)
    {
        output += node->getCar() + " ";
        node = node->getNextNode();
    }
    return output;
}

int main()
{
    char xit;
    //ser head node to NULL
    Node* headNode = NULL;

    //create node 1
    Node* node1 = new Node();
    node1->setCar("Mercedes");

    //create node 2
    Node* node2 = new Node();
    node2->setCar("BMW");

    //set node links
    headNode->setNextNode(node1);
    node1->setNextNode(node1);
    node2->setNextNode(node2);

    headNode = node1;

    Node* lastNode = findLast(headNode);

    lastNode->setNextNode(NULL);

    cout << toString(headNode) << endl;

    //pause console
    cin >> xit;
}
#包括
#包括
使用名称空间std;
类节点
{
私人:
串车;
节点*nextNode;
公众:
无效设置车(串车)
{
_汽车=汽车;
}
字符串getCar()
{
返回车;
}
void setNextNode(节点*节点)
{
nextNode=节点;
}
节点*getNextNode()
{
返回下一个节点;
}
};
Node*findLast(Node*Node)
{
Node*nodeOut=NULL;
while(node->getNextNode()!=NULL)
{
nodeOut=node->getNextNode();
}
回点头;
}
字符串到字符串(节点*节点)
{
字符串输出=”;
while(node->getNextNode()!=NULL)
{
输出+=节点->getCar()+“”;
node=node->getNextNode();
}
返回输出;
}
int main()
{
炭黑;
//ser头节点为空
Node*headNode=NULL;
//创建节点1
Node*node1=新节点();
node1->setCar(“梅赛德斯”);
//创建节点2
Node*node2=新节点();
node2->setCar(“宝马”);
//设置节点链接
headNode->setNextNode(节点1);
node1->setNextNode(node1);
node2->setNextNode(node2);
头节点=节点1;
Node*lastNode=findLast(headNode);
lastNode->setNextNode(空);
退出;
}

分配新的
节点时,指针
nextNode
没有初始化,它只是随机垃圾。您需要将其显式设置为
NULL
(可能在
节点的构造函数中)

也可以假定,标准C++库中有一个链接列表,而你只是为了学习而这样做;-)p> 请重新阅读以下内容:

node1->setNextNode(node1);
node2->setNextNode(node2);
…想想你在这里做什么


如果您要编写链表代码,我建议您至少查看
std::list
的接口。现在,您的接口处于非常低的级别,您至少可以直接操作指针。

实际错误的原因是:

headNode->setNextNode(node1);
headNode
仍然设置为NULL,因此您取消了对NULL指针的引用。正如Jerry所指出的,您也在调用让节点指向它们自己,这不是您想要的


如果您将汽车作为构造函数参数,它会更干净。

您需要重新查看代码

头节点=节点1

此分配应在访问实例头节点的任何成员函数之前完成。 最初您已为此指针指定NULL。 创建node1后,您将设置为无效实例的headNode。这就是撞车的原因。 确保你的目标,然后试着去实现。在纸上做一些粗略的工作,画一些图表,这样你就能更清楚地知道你到底想实现什么。 为什么设置下一个节点???我不理解你想要实现的目标。先说清楚

根据我的理解,该代码应如下实现

#include <iostream> 
#include <string> 

using namespace std; 

class Node 
{ 
    private: 
        string _car; 
        Node* nextNode; 

    public: 
        void setCar(string car) 
        { 
            _car = car; 
        } 

        string getCar() 
        { 
            return _car; 
        } 

        void setNextNode(Node* node) 
        { 
            nextNode = node; 
        } 

        Node* getNextNode() 
        { 
            return nextNode; 
        } 
}; 

Node* findLast(Node* node) 
{ 
    Node* nodeOut = node->getNextNode(); 
    while ( nodeOut->getNextNode()!= NULL) 
    { 
        nodeOut = nodeOut->getNextNode(); 
    } 
    return nodeOut; 
} 

string toString(Node* node) 
{ 
    string output = ""; 
    while (node != NULL) 
    { 
        output += node->getCar() + " "; 
        node = node->getNextNode(); 
    } 
    return output; 
} 

int main() 
{ 
    char xit; 
    //ser head node to NULL 
    Node* headNode = NULL; 

    //create node 1 
    Node* node1 = new Node(); 
    node1->setCar("Mercedes"); 
    node1->setNextNode(NULL);//Make null to each next node pointer 

    headNode = node1; //assign the node1 as headNode

    //create node 2 
    Node* node2 = new Node(); 
    node2->setCar("BMW"); 
    node2->setNextNode(NULL);

    //set node links 
     node1->setNextNode(node2);




    Node* lastNode = findLast(headNode); 

    lastNode->setNextNode(NULL); 

    cout << toString(headNode) << endl; 

    //pause console 
    cin >> xit; 
}
#包括
#包括
使用名称空间std;
类节点
{ 
私人:
串车;
节点*nextNode;
公众:
无效设置车(串车)
{ 
_汽车=汽车;
} 
字符串getCar()
{ 
返回车;
} 
void setNextNode(节点*节点)
{ 
nextNode=节点;
} 
节点*getNextNode()
{ 
返回下一个节点;
} 
}; 
Node*findLast(Node*Node)
{ 
Node*nodeOut=Node->getNextNode();
while(nodeOut->getNextNode()!=NULL)
{ 
nodeOut=nodeOut->getNextNode();
} 
回点头;
} 
字符串到字符串(节点*节点)
{ 
字符串输出=”;
while(节点!=NULL)
{ 
输出+=节点->getCar()+“”;
node=node->getNextNode();
} 
返回输出;
} 
int main()
{ 
炭黑;
//ser头节点为空
Node*headNode=NULL;
//创建节点1
Node*node1=新节点();
node1->setCar(“梅赛德斯”);
node1->setNextNode(NULL);//为下一个节点指针设置NULL
headNode=node1;//将node1指定为headNode
//创建节点2
Node*node2=新节点();
node2->setCar(“宝马”);
node2->setNextNode(空);
//设置节点链接
node1->setNextNode(node2);
Node*lastNode=findLast(headNode);
lastNode->setNextNode(空);
退出;
}

希望对初学者在C++中实现链接表是有用的。

< P>谢谢所有的建议,这是我在大清理后的最后代码:

// LinkedListProject.cpp : main project file.

#include "stdafx.h"

#include <iostream>
#include <string>

using namespace System;
using namespace std;

class Node
{
    public:
        Node()
            :_car(""), _nextNode(NULL)
        {
        }

        void SetCar(string car)
        {
            _car = car;
        }

        string GetCar()
        {
            return _car;
        }

        void SetNextNode(Node *node)
        {
            _nextNode = node;
        }

        Node * GetNextNode()
        {
            return _nextNode;
        }

    private:
        string _car;
        Node *_nextNode;
};

string GetData();
Node * AddNode(Node *firstNode, Node *newNode);
Node * DeleteNode(Node *firstNode, string nodeData);
void PrintNodes(Node *firstNode);

int main(int argc, char *argv[])
{
    string command = "";
    string data = "";
    Node *firstNode = NULL;

    do
    {
        cout << "Enter command: ";
        cin >> command;

        if(command == "add")
        {
            data = GetData();

            Node *newNode = new Node();
            newNode->SetCar(data);

            firstNode = AddNode(firstNode, newNode);
        }
        else if(command == "delete")
        {
            data = GetData();

            firstNode = DeleteNode(firstNode, data);
        }
        else if(command == "print")
        {
            PrintNodes(firstNode);
        }
    } while(command != "stop");

    return 0;
}

string GetData()
{
    string data = "";

    cout << "Enter data: ";
    cin >> data;

    return data;
}

Node * AddNode(Node *firstNode, Node *newNode)
{
    //add new node to front of queue
    newNode->SetNextNode(firstNode);
    firstNode = newNode;

    return firstNode;
}

Node * DeleteNode(Node *firstNode, string nodeData)
{
    Node *currentNode = firstNode;
    Node *nodeToDelete = NULL;

    if (firstNode != NULL)
    {
        //check first node
        if(firstNode->GetCar() == nodeData)
        {
            nodeToDelete = firstNode;
            firstNode = firstNode->GetNextNode();
        }
        else //check other nodes
        {
            while (currentNode->GetNextNode() != NULL &&
                   currentNode->GetNextNode()->GetCar() != nodeData)
            {
                currentNode = currentNode->GetNextNode();
            }

            if (currentNode->GetNextNode() != NULL &&
                currentNode->GetNextNode()->GetCar() == nodeData)
            {
                nodeToDelete = currentNode->GetNextNode();
                currentNode->SetNextNode(currentNode->GetNextNode()->GetNextNode());
            }
        }

        if(nodeToDelete != NULL)
        {
            delete nodeToDelete;
        }
    }

    return firstNode;
}

void PrintNodes(Node *firstNode)
{
    Node *currentNode = firstNode;
    while(currentNode != NULL)
    {
        cout << currentNode->GetCar() << endl;
        currentNode = currentNode->GetNextNode();
    }
}
//LinkedListProject.cpp:主项目文件。
#包括“stdafx.h”
#包括
#包括
使用名称空间系统;
使用名称空间std;
类节点
{
公众:
节点()
:_car(“”),_nextNode(NULL)
{
}
无效设置车(串车)
{
_汽车=汽车;
}
字符串GetCar()
{
返回车;
}
void SetNextNode(节点*节点)
{
_nextNode=节点;
}
节点*GetNextNode()
{
返回下一个节点;
}
私人:
串车;
节点*_下一个节点;
};
字符串GetData();
节点*AddNode(节点*firstNode,节点*newNode);
节点*删除节点(节点*第一个节点,字符串节点数据);
无效打印节点(节点*第一个节点);
int main(int argc,char*argv[])
{
string命令=”;
字符串数据=”;
Node*firstNode=NULL;
做
{
cout>命令;
如果(命令==“添加”)
{
data=GetData();
Node*newNode=newNode();
新建节点->设置车辆(数据);
firstNode=AddNode(firstNode,newNode);
}
else if(命令==“删除”)
{