I have the following structure for my dynamic list:
typedef struct DataNode
{
int id;
} DataNode;
typedef struct Node
{
DataNode data;
struct Node *Next;
} Node;
typedef struct List
{
int size;
Node *head;
} List;
For this structure, I have two methods:
Create List Method:
List *CreateList ()
{
List *list = (List*) malloc(sizeof(List));
list->size = 0;
list->head = NULL;
return list;
}
On the list->head = NULL
line, will all elements inside the *head
(Node type) pointer receive the NULL value? The id
variable and the *Next
pointer will be NULL?
Push method:
void Push (List *list, DataNode data01)
{
Node *node = (Node*) malloc (sizeof(Node));
node->data = data01;
node->Next = list->head;
list->head = node;
list->size++;
}
Why does the pointer of the next node receive the address of the node itself ( node->Next = list->head;
)? In the next line the node receives the address of the *node
pointer (type Node) ( list->head = node;
). When it creates the *node
pointer, is it creating the node in the list? And when the *head
pointer gets the address of the *node
pointer, is it receiving the node address?
Thank you.