So I'm really confused. I am trying to write a c method that allows me to add a new "node" to the top of a linked list. I have done this before in C ++ and no problem. I am upset because after writing the code I was sure that I was right, I went and looked around, and basically wherever I find it says that I am doing the same thing as everything that I am ready ... I I will give the code and step-by-step addresses and values ββof variables. Here is the code:
Real function:
void addToBeginning(int value, struct node* root){
struct node* newNode;
newNode = malloc( sizeof(struct node));
newNode->value = value;
newNode->next = root;
root = newNode;
}
But here is the full code. I removed some things to make it more concise (things that are not needed to answer this question, for example getLength (...) and addToPos (...))
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node* next;
};
void printLinkedList(struct node* root);
void addToEnd(int value, struct node* root);
void addToBeginning(int value, struct node* root);
void addToPos(int pos, int value, struct node* root);
int getLength(struct node* root);
int main(){
struct node *root2;
root2 = malloc( sizeof(struct node));
root2->value = 4;
root2->next = NULL;
i = 0;
while (i < 5){
addToEnd(0,root2);
i++;
}
printLinkedList(root2);
addToBeginning(2, root2);
printLinkedList(root2);
return(0);
}
void printLinkedList(struct node* root){
while(root != NULL){
if (root->next != NULL){
printf("%d, ",root->value);
root=root->next;
} else {
printf("%d\n",root->value);
root=root->next;
}
}
}
void addToEnd(int value, struct node* root){
struct node* newNode = malloc( sizeof(struct node));
newNode->value = value;
newNode->next = NULL;
if (root->next == NULL){
root->next = newNode;
} else {
struct node* current = root;
while(current->next != NULL){
current = current->next;
}
current->next = newNode;
}
}
void addToBeginning(int value, struct node* root){
struct node* newNode;
newNode = malloc( sizeof(struct node));
newNode->value = value;
newNode->next = root;
root = newNode;
}
What is confusing, I feel like there is a problem with an alias on
newNode->next = root;
root = newNode;
... , :
... addToBeginning(int value, struct node* root){...} :
:
struct node* newNode;
newNode = malloc( sizeof(struct node));
root newNode:
root = 0x0000000100103c60
root->next = 0x0000000100103c70
root->value = 4
newNode = 0x0000000100103cc0
newNode->next = NULL
newNode->value = 0
:
newNode->value = value;
root newNode:
root = 0x0000000100103c60
root->next = 0x0000000100103c70
root->value = 4
newNode = 0x0000000100103cc0
newNode->next = NULL
newNode->value = 2
:
newNode->next = root;
root newNode:
root = 0x0000000100103c60
root->next = 0x0000000100103c70
root->value = 4
newNode = 0x0000000100103cc0
newNode->next = 0x0000000100103c60
newNode->value = 2
:
root = newNode;
root newNode:
root = 0x0000000100103cc0
root->next = 0x0000000100103c60
root->value = 2
newNode = 0x0000000100103cc0
newNode->next = 0x0000000100103c60
newNode->value = 2
, , *root , , 0x0000000100103c60, , , .