简体   繁体   中英

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

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? Doesn't that mean that the node loses the address of the next node, meaning its address in cur ->next is zero?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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