【问题标题】:How to add elements to the end of a linked list如何将元素添加到链表的末尾
【发布时间】:2013-12-21 11:21:24
【问题描述】:

所以我试图在 C++ 中创建一个函数,它应该在链表的末尾添加一个元素。 这个添加元素的函数应该在main-方法中调用。 即使列表中没有元素也应该可以调用它。

到目前为止,我有以下内容:

int main()
{
   ListElement* l = new ListElement;
   l->digit = 9;
   l->next = NULL;
   appendList(l, 5);
   appendList(l, 7);

   printList(l);

   return 0;
}

void appendList(ListElement *&l, int newDigit)
{
    ListElement *lh = new ListElement;

    if(l == NULL)
    {
        l->digit = newDigit;
        l->next = NULL;
    }
    else
    {
        lh=l;

        while(lh != NULL)
        {
           lh=lh->next;
        }
        lh=lh->next;
        lh->digit = newDigit;
        lh->next = NULL;
        l = lh;
    }
}

不幸的是,它不起作用。我试过删除或添加参数 - 没有任何帮助,我在互联网上找不到合适的答案。 所以如果你们中的任何人可以帮助我,我会非常非常高兴,因为我在这里有点绝望......

【问题讨论】:

  • 你想循环直到 lh->next == NULL 而不是 lh == NULL。

标签: c++ list pointers linked-list singly-linked-list


【解决方案1】:

仔细看:

if (l == NULL)
{
    l->digit = newDigit;
    l->next = NULL;
}

l == NULL .... l->digit 您正在解除对 NULL 指针的引用!

另一个问题是您在ListElement *lh = new ListElement; 中分配lh,然后在“else”块lh=l; 中立即用l 覆盖其值。

试试这样的方法:

#include <cassert>
#include <iostream>

struct ListElement final {
    explicit ListElement(int digit) : digit{digit} {}

    int digit = 0;
    ListElement* next = nullptr;
};

void appendList(ListElement*& l, int newDigit);
void printList(ListElement* l);
void freeList(ListElement* l);

int main() {
    ListElement* l{nullptr};
    appendList(l, 9);
    appendList(l, 5);
    appendList(l, 7);
    printList(l);
    freeList(l);
}

void appendList(ListElement*& l, int newDigit) {
    if (!l) {
        // Creating first element of the list.
        l = new ListElement{newDigit};
        return;
    }

    // Since we got a single linked list and don't have a pointer
    // to its tail, iterate over the list to get the last element
    // to append to...
    auto tail = l;
    while (tail->next)
        tail = tail->next;
    assert(tail->next == nullptr);
    tail->next = new ListElement{newDigit};
}

void printList(ListElement* l) {
    unsigned int index = 0;
    while (l != nullptr) {
        std::cout << index++ << ": " << l->digit << '\n';
        l = l->next;
    }
}

void freeList(ListElement* l) {
    ListElement* tmp;
    while (l != nullptr) {
        tmp = l;
        l = l->next;
        delete tmp;
    }
}

【讨论】:

    【解决方案2】:

    试试下面的

    void appendList( ListElement * &head, int newDigit )
    {
        if ( head == 0 )
        {
            head = new ListElement;
    
            head->digit = newDigit;
            head->next  = 0;
        }
        else
        {
            ListElement *next = head;
    
            while ( next->next ) next = next->next;
    
            next->next = new ListElement;
            next->next->digit = newDigit;
            next->next->next  = 0;
        }
    } 
    
    
    int main()
    {
        ListElement *head = 0;
    
        appendList( head, 9 );
        appendList( head, 5 );
        appendList( head, 7 );
    
        printList( head );
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-13
      • 2012-09-12
      • 2020-09-11
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 2023-01-12
      相关资源
      最近更新 更多