访问冲突读取位置0xC0000005 C ++

时间:2013-04-09 02:51:05

标签: c++

我的添加功能显然有问题,因为它首先取消引用,因此首先指向的是什么。我只是不知道如何解决它,因此它不是一个空指针。

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

3 个答案:

答案 0 :(得分:6)

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->nextNULL

        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 ++;

first->data = item中的if是多余的。 firstlast相同,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。 :)

答案 1 :(得分:3)

if (first == NULL)
{
    /* if first is NULL dereference it. Hooray! */
    first->data = item;
    ...

答案 2 :(得分:0)

查看linked list

有一些细节,首先需要先创建first == NULL,然后将其插入链接列表并将其挂钩,请参阅某些算法的链接文章。

我想说最简单的是带有标题节点(而不是first *)的单链表,它可以指向自身,但是有很多方法可以实现链表,这取决于你选择的方法如何挂钩元素。

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