Printing linklist

I am not getting the right output when I print the linklist
I am always getting 2 garbage values.
Here is my code

class node{
public:
int data;
node* next;
node(int d){
data = d;
next = NULL;
}};

void insertAtHead(node &head, int data)
{
node
n = new node(data);
n->next = head;
head = n;
}

int length (node head){
int l = 0;
while (head->next != NULL){
head = head->next;
l += 1;
cout<<l<<endl;
}
return l;
}
void print(node
&head){
int a = 1;
while(head != NULL){
cout <<"-> ";
cout<data<<endl;
head = head->next;
}
cout<<endl;
}
int main(){
node *head;
insertAtHead(head,5);
insertAtHead(head,4);
insertAtHead(head,3);
insertAtHead(head,2);
insertAtHead(head,1);

print(head);
return 0;

}

Hi @15abhinavgarg, you send me the code using coding blocks ide . save it and share the link here.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.