简体   繁体   English

有人可以向我解释这个链表是如何工作的吗?

[英]Can someone explain to me how this linked list works?

A linked list is coded below.一个链表编码如下。 It's very rudimentary, but I don't quite understand something.这是非常初级的,但我不太明白一些事情。

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <time.h>
#include <fstream>

using namespace std;

int main()
{

    struct node {
        int data;
        node* next;
    };
    node* head = NULL;
    node* cur = NULL;
    string condition = "Yes";
    while (condition != "No") {
        cout << "What is the next value of the linked list " << endl;
        int value;
        cin >> value;
        node* newnode = new node;
        newnode->data = value;
        newnode->next = NULL;
        

        if (head == NULL) {
            head = newnode;
            cur = newnode;
            newnode = NULL;
        }
        else {
            cur->next = newnode;
            cur = newnode;
    
        }

        
        cin >> condition;
    }
    cout << "These are your values " << endl;
    int i = 0;
    while (i < 7) {
        cout << head->data << endl;
        head = head->next;
        i++;
    }

}

In the function, when its supposed to add a new value to the linked list, it goes在 function 中,当它应该向链表添加一个新值时,它会

else {
            cur->next = newnode;
            cur = newnode;
    
        }

But to my understanding, if I change the value of next to the pointer of newnode in the first line, doesn't me changing the entire node in the second line overwrite that?但据我了解,如果我在第一行中更改 newnode 指针旁边的值,我在第二行中更改整个节点不会覆盖它吗? Doesn't that mean that the node loses the address of the next node, meaning its address in cur ->next is zero?这是否意味着该节点丢失了下一个节点的地址,这意味着它在 cur ->next 中的地址为零?

Can someone explain to me how this works?有人可以向我解释这是如何工作的吗?

the var cur is only a pointer, the first time it point to last newnode, and cur->next'address is current newnode, when execute second line, the cur address changed, but last newnode has no change, thus cur->next will not be zero, it points to the current newnode. var cur只是一个指针,第一次指向最后一个newnode,cur->next'address是当前newnode,执行第二行时,cur地址改变了,但是last newnode没有变化,因此cur->next不会为零,它指向当前的新节点。

I suggest you to change the way you iterate your linked list.我建议您更改迭代链表的方式。 It is not a proper way.这不是正确的方法。 The way you are doing also makes it look more complicated than is actually is.你做的方式也让它看起来比实际上更复杂。

You can either use a loop or a recursive function.您可以使用循环或递归 function。 Here are some examples这里有些例子

void linkedList::insert(int val){
    
    if(root == nullptr){
        root = new Node;
        root->data = val;
        root->next = nullptr;
    } else {
        addUp(val);
    }
}

void linkedList::addUp(int val){
    Node *temp = new Node;
    Node *head = root;
    
    while(head != nullptr){
        temp = head;
        head = head->next;
    }
    Node *node = new Node;
    node->data = val;
    node->next = nullptr;
    temp->next = node;
}

Recursive递归的

Node* head = NULL; 
head = insertEnd(head, 6); 

Node *newNode(int data) 
{ 
    Node *new_node = new Node; 
    new_node->data = data; 
    new_node->next = NULL; 
    return new_node; 
} 
  
Node* insertEnd(Node* head, int data) 
{ 
    if (head == NULL)  
         return newNode(data); 
    else 
        head->next = insertEnd(head->next, data); 
    return head; 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM