C ++链接列表运行时错误:未处理的异常 - 写入位置违规

时间:2010-09-10 04:31:45

标签: c++ pointers linked-list

我正在尝试在C ++中构建自己的链表实现。我的代码正在编译,但显然我的指针存在一些问题,指的是无效的内存地址。

这是我的实施:

#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;
}

5 个答案:

答案 0 :(得分:1)

重读:

node1->setNextNode(node1);
node2->setNextNode(node2);

......想想你在这里做了什么。

如果您要编写链接列表代码,我建议至少查看std::list接口。现在,你的界面处于如此低的水平,你至少可以直接操纵指针。

答案 1 :(得分:1)

您实际错误的原因是:

headNode->setNextNode(node1);

headNode仍设置为NULL,因此您将取消引用NULL指针。正如Jerry所指出的,你也在呼叫节点指向自己,这不是你想要的。

如果你将汽车作为构造函数参数,它会更清晰。

答案 2 :(得分:1)

您需要重新查看代码。

headNode = node1;

此分配应在访问实例headNode的任何成员函数之前完成。 最初,您已为此指针指定了NULL。 创建node1后,您将设置为headNode,这是无效的实例。这是崩溃的原因。 确保你的目标,然后尝试在纸上做一些粗略的工作,制作一些图表,你会更清楚你正在努力实现的目标。 为什么setNextNode ???我不会理解你想要实现的目标。先说清楚。

根据我的承诺,此代码应如下所示实施..

#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; 
}

希望对于在c ++中实现链接列表的初学者有用。

答案 3 :(得分:0)

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

另外,我假设您知道标准C ++库内置了一个链接列表,而您只是为了学习而这样做; - )

答案 4 :(得分:0)

感谢所有的建议,这是我在重大清理后的最终代码:

// 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();
    }
}
相关问题