Issues creating and iterating through Linked List?

后端 未结 5 1324
眼角桃花
眼角桃花 2021-01-15 20:10

When I run the program below, it prints \"one: 1\", instead of \"one : 1, two: 2\", as I had expected. Anyone know what\'s going on here? I am trying to create a function th

相关标签:
5条回答
  • 2021-01-15 20:43

    Without seeing your iterate function the problem is that head1 is pointing to the last element on the list, not the first. You probably meant the the last line of the add function to be:

    return head;
    

    and 2nd line of your program to be:

    head1 = add(2, "two", head1);
    

    But that will still probably give you your output in the wrong order, because your add function is adding the new element to the start of the list, not the end.

    0 讨论(0)
  • 2021-01-15 20:43

    To understand the problem in detail, please go through The C Programming Language by Kernighan & Ritchie - Introduction (chapter 1.7/1.8) and Functions and Program Structure (chapter 4).

    Hint: pass-by-value & pass-by-address

    int main() {
        ...
        add(2, "two", &head1);
        ...
    }
    

    OTOH, you should validate the return address of malloc before de-referencing it (in createNewLinkedList).

    0 讨论(0)
  • 2021-01-15 20:56

    You are passing a pointer to the node. Like all parameters in C, it is passed by value, therefore head = newNode; has no effect in the caller.

    You need to change the signature to accept node **head, and add a level of indirection in order to make the changes in add be reflected in the main.

    int add(int data, char name[], node **head) {
        node *newNode = (node *)malloc(sizeof(node));
        if (newNode != NULL) {
            newNode->value = data;
            strcpy(newNode->label, name);
            newNode->next = *head;
            *head = newNode;
        }
    }
    

    Of course you'll need to pass &head1 to the add method: add(2, "two", &head1);

    P.S. Since you are adding {2,"two"} to the front of the list, your output will be "two: 2, one : 1"

    0 讨论(0)
  • 2021-01-15 20:57

    head = newNode; will not do what you expect.

    head is pass-by-value here. Think about it.

    0 讨论(0)
  • 2021-01-15 21:06

    In add, you're not connecting the new node to the end of your list, you're merely creating a new node that's completely disconnected from your actual list. You also don't return an int from that function as per the function's signature.

    I'm suspecting that you're wanting to set the new node as the head with this line:

    head = newNode;
    

    If that's the case, change head to type node** and do *head = newNode, otherwise you're just modifying a local variable to point to newNode, not the actual head1 pointer being passed from main. In main you would then pass &head1 to add.


    EDIT: What would probably make more sense is to have head->next = newNode;, or to iterate to the end of the list and set the next value of the node which is pointing to NULL to newNode. ie.

    node *lastNode = head;
    while (lastNode->next != NULL) /* get to the last node in the list */
       lastNode = lastNode->next;
    
    lastNode->next = newNode; /* add newNode to the end of the list */
    

    Also have newNode->next = NULL instead of newNode->next = head. Now the output will be "one : 1, two: 2", instead of "two: 2, one : 1" that would have happened if you added the newNode to the head.

    0 讨论(0)
提交回复
热议问题