I am attaching below the code of head_insert
to insert a new node at the head of a linked list. The function is called with head_insert(head)
.
I am not sure about the syntax of the first argument of the function, I was expecting NodePtr
instead since it is already a pointer, see below.
Why the code uses NodePtr &head
and not NodePtr head
only as head is already a pointer?
void head_insert(NodePtr & head, int the_number)
{
NodePtr temp_ptr;
temp_ptr=new Node;
temp_ptr->data=the_number;
temp_ptr->link=head;
head=temp_ptr;
}
struct Node
{
int data;
Node *link;
};
typedef Node* NodePtr;