在C++中,将节点添加到链表的末尾。

huangapple go评论95阅读模式
英文:

Linked list in c++ adding node at the end of the list

问题

以下是您提供的代码的翻译:

我正在编写一个将元素存储在链表中的代码,但是当我在列表末尾添加一个元素时,输出始终如下...

[在此输入图像描述](https://i.stack.imgur.com/ApWie.png)
我已经从许多教程中搜索并复制了它们的代码,以查看它是否在我的代码上运行,但是它不起作用...

#include <iostream>
#define tab '\t'

using std::endl;

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

class Linkedlist {
private:
    node* head = new node;

public:
    Linkedlist(){
        head->next = NULL;
    }

    void addStart(int val){
        node* temp = new node;
        temp->data = val;
        temp->next = head;
        head = temp;
    } 
    
    void addEnd(int val){
        node* temp = new node;
        temp->data = val;
        temp->next = NULL;

        node* curr = new node;
        curr = head;
        while(curr->next != NULL){
            curr = curr->next;
        }
        curr->next = temp;
    } 

    void display(){
        if (head == NULL){
            std::cout << endl << "Linked List is EMPTY.";
        } else {
            int i = 1;
            node* curr = new node;
            curr = head;

            std::cout << endl << "The linked list contains..." << endl;
            while(curr->next != NULL){
                std::cout << "Elements " << i << ':' 
                    << tab << curr->data << endl;
                curr = curr->next;
                i++;
            }
        }
    }
};

输出应该是...
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4

英文:

I am writing a code which will store elements in linked list, however when i'm adding an element at the end of the list the output is always like this...

enter image description here
i've already search from many tutorials and copying their code to see if it works on mine but it doesn't..


#include &lt;iostream&gt;
#define tab &#39;\t&#39;

using std::endl;

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

class Linkedlist {
private:
    node* head = new node;

public:
    Linkedlist(){
        head-&gt;next = NULL;
    }

    void addStart(int val){
        node* temp = new node;
        temp-&gt;data = val;
        temp-&gt;next = head;
        head = temp;
    } 
    
    void addEnd(int val){
        node* temp = new node;
        temp-&gt;data = val;
        temp-&gt;next = NULL;

        node* curr = new node;
        curr = head;
        while(curr-&gt;next != NULL){
            curr = curr-&gt;next;
        }
        curr-&gt;next = temp;
    } 

    void display(){
        if (head == NULL){
            std::cout &lt;&lt; endl &lt;&lt; &quot;Linked List is EMPTY.&quot;;
        } else {
            int i = 1;
            node* curr = new node;
            curr = head;

            std::cout &lt;&lt; endl &lt;&lt; &quot;The linked list contains...&quot; &lt;&lt; endl;
            while(curr-&gt;next != NULL){
                std::cout &lt;&lt; &quot;Elements &quot; &lt;&lt; i &lt;&lt; &#39;:&#39; 
                    &lt;&lt; tab &lt;&lt; curr-&gt;data &lt;&lt; endl;
                curr = curr-&gt;next;
                i++;
            }
        }
    }
};

The Output should be..
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4

答案1

得分: 2

如果您在空列表上使用 addEndhead 将指向 NULL。然后,当您执行 curr = head 时,curr 也是 NULL。接下来,您执行 while (curr-&gt;next != NULL),这是非法的,因为 currNULL,所以 curr-&gt;next 指向未知的内存。

您可以添加一个条件来检查 head 是否为 NULL

void addEnd(int val) {
    if (head == NULL) {
        this-&gt;addStart(val);
        return;
    }
    //其余的代码
}

上述代码检查了 head 是否为 NULL。如果是,表示列表为空,将元素添加到列表末尾在功能上等同于将元素添加到列表开头。在这种情况下,您可以只使用 addStart

英文:

If you use addEnd on an empty list, head will point to NULL. Then, when you do curr = head, curr is also NULL. Then, you do while (curr-&gt;next != NULL), which is illegal since curr is NULL, so curr-&gt;next points to unknown memory.

You can add a condition checking if head is NULL:

void addEnd(int val) {
    if (head == NULL) {
        this-&gt;addStart(val);
        return;
    }
    //the rest of the code
}

The above code checks if head is NULL. If it is, the list is empty, and adding an element to the end of the list is functionally equivalent to adding an element to its start. You can just use addStart in that case.

答案2

得分: 1

Bugs:

  1. 在addStart()函数中:

问题:这段代码从不将最后一个节点的next设置为NULL
解决方案:
a) 将head声明为null
b) 为两种不同的情况编写代码(当head为NULL和不为NULL时):

Linkedlist(){
    head = NULL;
}

void addStart(int val){
    node* temp = new node;
    temp->data = val;

    if(head == NULL){
        head = temp;
        head->next = NULL;
        return;
    }  else {
        temp->next = head;
        head = temp;
        return;
    }
}
  1. 您没有显示最后一个节点。
    解决方案:将display函数中的while循环条件从
curr->next != NULL

更改为

curr != NULL

整体代码:

#include <iostream>
#define tab '\t'

using std::endl;

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

class Linkedlist {
private:
    node* head = new node;

public:
    Linkedlist(){
        head = NULL;
    }

    void addStart(int val){
        node* temp = new node;
        temp->data = val;

        if(head == NULL){
            head = temp;
            head->next = NULL;
            return;
        }  else {
            temp->next = head;
            head = temp;
            return;
        }
    }

    void addEnd(int val){
        node* temp = new node;
        temp->data = val;
        temp->next = NULL;

        node* curr = new node;
        curr = head;

        while(curr->next != NULL){
            curr = curr->next;
        }
        curr->next = temp;
    }

    void display(){
        if (head == NULL){
            std::cout << endl << "Linked List is EMPTY.";
        } else {
            int i = 1;
            node* curr = new node;
            curr = head;

            std::cout << endl << "The linked list contains..." << endl;
            while(curr != NULL){
                std::cout << "Elements " << i << ':' 
                    << tab << curr->data << endl;
                curr = curr->next;
                i++;
            }
        }
    }
};
英文:

Bugs:

  1. at addStart() function:

> temp->next = head;
> head = temp;

Problem: This code never kept the last nodes' next as NULL
Solution:
a) declared head as null
b) writing code for 2 different conditions (when the head is NULL and not NULL):

 Linkedlist(){
        head= NULL;
    }

    void addStart(int val){
        node* temp = new node;
        temp-&gt;data = val;
        
        if(head == NULL){
            head = temp;
            head-&gt;next = NULL;
            return;
        }  else {
            temp-&gt;next = head;
            head = temp;
            return;
        }

           }
  1. You didn't display the last node.
    Soln: change while loop condition in display function from

> curr->next != NULL

to

> curr != NULL.

OVERALL CODE:

  #include &lt;iostream&gt;
    #define tab &#39;\t&#39;
    
    using std::endl;
    
    struct node {
        int data;
        node* next;
    };
    
    class Linkedlist {
    private:
        node* head = new node ;
        
       
    
    public:
        Linkedlist(){
            head= NULL;
        }
    
        void addStart(int val){
            node* temp = new node;
            temp-&gt;data = val;
            
            if(head == NULL){
                head = temp;
                head-&gt;next = NULL;
                return;
            }  else {
                temp-&gt;next = head;
                head = temp;
                return;
            }
    
               } 
        
        void addEnd(int val){
            node* temp = new node;
            temp-&gt;data = val;
            temp-&gt;next = NULL;
    
           node* curr = new node;
            curr = head;
    
           
            while(curr-&gt;next != NULL){
                curr = curr-&gt;next;
             
            }
            curr-&gt;next = temp;
           
        } 
    
        void display(){
            if (head == NULL){
                std::cout &lt;&lt; endl &lt;&lt; &quot;Linked List is EMPTY.&quot;;
            } else {
                int i = 1;
                node* curr = new node;
                curr = head;
    
                std::cout &lt;&lt; endl &lt;&lt; &quot;The linked list contains...&quot; &lt;&lt; endl;
                while(curr != NULL){
                    std::cout &lt;&lt; &quot;Elements &quot; &lt;&lt; i &lt;&lt; &#39;:&#39; 
                        &lt;&lt; tab &lt;&lt; curr-&gt;data &lt;&lt; endl;
                    curr = curr-&gt;next;
                    i++;
                }
            }
        }
    };

huangapple
  • 本文由 发表于 2023年7月4日 21:26:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定