Reputation: 805
I am new to C and I want to implement the linked list. This is my initial copied code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data; // integer data
struct Node* next; // pointer to the next node
} Node;
int main() {
Node* A = NULL;
Node* temp = malloc(sizeof * temp);
temp->data = 2;
temp->next = NULL;
A = temp;
printf("%d", A);
return 0;
}
I have understood how pointers work, for example:
//Example 2
int a = 2;
int* p = &a;
such that p
holds the address of a
and *p
holds the content of it.
In the node example, the basic idea is to create an initial node, and then from there, to link other nodes when inserting at the end for instance. So, when we did this:
Node* A = NULL;
Node* temp = malloc(sizeof * temp);
we create a node A
, my first question here, why I can't use the same concept of accessing its address and content NULL
the same as in Example 2
or how can I do that?
Second, when we created node temp
and assigned 2
to its data, and NULL
to its next, it's all clear, but when we did A = temp
, this is not clear, what did we assigned exactly? I mean, how can I get from A
to the next node, A
now has A->data = 2
and A->next = NULL
, I was expecting A->next
to store the address of temp
, no? Please if you can explain in the simplest terms the basic abstract inner workings?
Update
For example, if I want to create a linked list with two elements 2
and 3
. Is the following code correct?
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data; // integer data
struct Node* next; // pointer to the next node
} Node;
int main() {
Node* A = NULL;
Node* temp = malloc(sizeof * temp);
temp->data = 2;
temp->next = NULL;
A->next = temp;
Node* temp1 = malloc(sizeof * temp1);
temp1->data = 3;
temp1->next = NULL;
temp->next = temp1;
return 0;
}
Upvotes: 0
Views: 89
Reputation: 385575
when we did A = temp, this is not clear, what did we assigned exactly?
temp
contains the the address of the node allocated by malloc
.
You assigned this to A
. (A = temp;
)
So A
contains the the address of the node allocated by malloc
.
Now can I get from A to the next node
What next node? You only ever created one node.
I was expecting A->next to store the address of temp, no?
At no point do you get the address of temp
. And there's no point in doing that. So I think you mean the address of the node (the address in temp
). But why wou ld that be in A->next
? You never assign temp
to A->next
. You assign NULL
to A->next
(temp->next = NULL;
).
Please if you can explain in the simplest terms the basic abstract inner workings?
temp
holds the address of the node you allocated.A
, so it holds the same address.*temp
and *A
is the node you allocated.(*temp).data
aka temp->data
is its data
field (currently containing 2
).A
has the same value as temp
, (*A).data
aka A->data
can also be used to access its data
field.(*temp).next
aka temp->next
is the address of the next node (currently NULL
).A
has the same value as temp
, (*A).next
aka A->next
can also be used to access its next
field.Node *temp Node anon
@ 0x1000 @ 0x2000
+------------+ +------------+
| 0x2000 ---------+---->| data: 2 |
+------------+ | | next: NULL |
| +------------+
Node *A |
@ 0x1004 |
+------------+ |
| 0x2000 ---------+
+------------+
(The addresses are completely made up, of course.)
Is the following code correct?
No. You have:
Node* A = NULL;
...
A->next = temp;
A
is NULL
. It doesn't point to a node. So attempting to set the next
field of the node to which A
points makes no sense. This is undefined behaviour.
Upvotes: 1