1

我的 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 ++;
            }
        }
4

3 回答 3

6

if和之间有很多共同的代码else

        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是多余的。 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 ++;

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

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

该代码现在也应该更容易理解了。教训:不要重复自己。:)

于 2013-04-09T03:21:21.593 回答
3
if (first == NULL)
{
    /* if first is NULL dereference it. Hooray! */
    first->data = item;
    ...
于 2013-04-09T02:53:55.650 回答
0

看看链表

细节很少,首先first == NULL需要先创建,将其插入链表并连接起来,有关一些算法,请参见链接文章。

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

这取决于您所追求的,但是如果您只需要一些工作,那么您可以从boost 侵入式循环 slist 算法中学习,您只需使用数据和下一个指针定义自己的结构,告诉它如何访问下一个并使用提供的算法来做所有工作(链接和取消链接节点)。

于 2013-04-09T03:25:56.193 回答