I am currently learning linked list and my prof sent us a code which is so hard to understand for me. I know that asterisk is used before the varaible to make it as a pointer but this one is infront of a variable.
Here is the code:
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node *head = NULL;
void insert(int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head;
head = new_node;
}
void display() {
struct Node* ptr;
ptr = head;
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The linked list is: ";
display();
return 0;
}
This is the one that I am talking about:
void insert(int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head;
head = new_node;
}
I dont know what is the purpose of the asterisk in here (struct Node*) malloc(sizeof(struct Node));
\
and can someone tell me what is the purpose of malloc here malloc(sizeof(struct Node))