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)
{
noden = 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;
}