【问题标题】:Access violation reading location 0xC0000005 C++访问冲突读取位置 0xC0000005 C++
【发布时间】:2013-04-09 02:51:04
【问题描述】:

我的 add 函数显然有问题,因为它首先取消引用,因此首先指向任何内容。我只是不知道如何修复它,使其不是空指针。

    struct Node
    {
        int data;
        Node *next;
    };

    class LinkedList
    {
        Node *first;
        Node *last;
        int count;
        public:

        LinkedList()
        {
            first = NULL;
            last = NULL;
            count = 0;
        }


        void Add(int item)
        {
            if (first == NULL)
            {
                first->data = item;
                last->data = item;
                last->next = NULL;
                first->next = last;
                count = 1;
            }
            else
            {
                Node *newNode = new Node;
                newNode->data = last->data;
                newNode->next = last;
                last->data = item;
                last->next = NULL;
                count ++;
            }
        }

【问题讨论】:

  • 如果您不希望 first 为空,请将其指向某个对象。您已经知道如何创建Node(如您的else 块中所示),所以:first = new Node;
  • 你的条件是if first is null, then use first。这是不正确的。如果 first 为 null,则不能使用它。
  • 你是认真的吗?再开一个帖子怎么样?
  • @BdkFivehunna 我将您的问题回滚到其原始形式。如果您有不同的问题,请提出一个新问题。不要替换现有的问题。这样做会使所有的 cmets 和答案看起来毫无意义。
  • @BdkFivehunna 请停止替换您的问题。

标签: c++


【解决方案1】:

ifelse 之间有很多共同的代码。

        if (first == NULL)
        {
            first->data = item;
            last->data = item;
            last->next = NULL;
            first->next = last;
            count = 1;
        }
        else
        {
            Node *newNode = new Node;
            newNode->data = last->data;
            newNode->next = last;
            last->data = item;
            last->next = NULL;
            count ++;
        }

if 中,您将count0 增加到1。在else 中,您增加它。

count总是递增。所以你不需要输入两次。

        if (first == NULL)
        {
            first->data = item;
            last->data = item;
            last->next = NULL;
            first->next = last;
        }
        else
        {
            Node *newNode = new Node;
            newNode->data = last->data;
            newNode->next = last;
            last->data = item;
            last->next = NULL;
        }
        count ++;

您还在它们中将last->data 设置为item

而且你在他们两个中都将last->next 设置为NULL

        if (first == NULL)
        {
            first->data = item;
            first->next = last;
        }
        else
        {
            Node *newNode = new Node;
            newNode->data = last->data;
            newNode->next = last;
        }
        last->data = item;
        last->next = NULL;
        count ++;

当它是第一个新节点时,您还忘记创建 new Node

        if (first == NULL)
        {
            Node *newNode = new Node;   // Added
            first = newNode;            // Added
            last = newNode;             // Added
            first->data = item;
            first->next = last;
        }
        else
        {
            Node *newNode = new Node;
            newNode->data = last->data;
            newNode->next = last;
        }
        last->data = item;
        last->next = NULL;
        count ++;

if 中的 first->data = item 是多余的。 first 与那里的last 相同,并且last->data = item 已经发生了。

        if (first == NULL)
        {
            Node *newNode = new Node;
            first = newNode; 
            last = newNode;
            // Removed
            first->next = last;
        }
        else
        {
            Node *newNode = new Node;
            newNode->data = last->data;
            newNode->next = last;
        }
        last->data = item;
        last->next = NULL;
        count ++;

由于firstnewNodeif 中具有相同的值,我们可以互换使用变量名。

        if (first == NULL)
        {
            Node *newNode = new Node; 
            first = newNode;            // These two pointers are equal!
            last = newNode;
            newNode->next = last;       // (same pointer)
        }
        else
        {
            Node *newNode = new Node;
            newNode->data = last->data;
            newNode->next = last;
        }
        last->data = item;
        last->next = NULL;
        count ++;

现在,else 中的几乎所有内容也在您的 if 中。它可以全部移出。

        Node *newNode = new Node; 
        if (first == NULL)
        {
            first = newNode;
            last = newNode;
        }
        else
        {
            newNode->data = last->data;
        }
        newNode->next = last;
        last->data = item;
        last->next = NULL;
        count ++;

该代码现在也应该更容易理解了。课程:Don't Repeat Yourself。 :)

【讨论】:

  • 优秀的帖子有很多细节和很好的解释,所以 OP 可以跟进。 +1
【解决方案2】:
if (first == NULL)
{
    /* if first is NULL dereference it. Hooray! */
    first->data = item;
    ...

【讨论】:

  • 我认为 OP 可能已经知道这一点(“它首先取消引用并且首先指向任何内容”)但不知道如何修复它。
  • 简单的first = new Node;怎么样?
  • @BdkFivehunna 你需要一个new Node,不管它是否为NULL。
  • @jamesdlin 考虑到循环的 else 部分分配了一个新节点,这似乎不太可能。但你永远不知道。
  • @NikBougalis:实际上OP回复了这个答案并确认了我所说的,但由于某种原因删除了评论。
【解决方案3】:

看看linked list

细节不多,首先要先创建first == NULL,插入链表并挂接,部分算法见链接文章。

我想说最简单的是带有一个可以指向自身的头节点(而不是first *)的单链表,但是实现链表的方法有很多,这取决于您选择如何连接元素。

这取决于你所追求的,但如果你只需要一些工作,那么你可以从boost intrusive circular slist algorithms 中获取,你只需使用数据和下一个指针定义你自己的结构,告诉它如何访问下一个并使用提供的算法来完成所有工作(链接和取消链接节点)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多