My Cpp File code
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
void insert_at_end(Node *head, int data)
{
Node *temp = new Node();
temp->data = data;
temp->next = NULL;
if (head == NULL)
{
head = temp;
// cout << temp->data << " " << " : " << head->data << endl ;
}
else
{
Node *last = head;
while (last->next != NULL)
{
last = last->next;
}
last->next = temp;
cout << "Inserted " << data << " at the End \n";
}
}
void printList(Node *head)
{
cout << "List : \n";
Node *temp = head;
if (temp == NULL)
cout << "Forgive me !";
while (temp != NULL)
{
cout << "\t" << temp->data << "";
temp = temp->next;
}
}
int main()
{
Node *head = NULL;
insert_at_end(head, 12);
insert_at_end(head, 16);
insert_at_end(head, 71);
insert_at_end(head, 81);
insert_at_end(head, 91);
printList(head);
return 0;
}
It works fine if Head is not NULL ( If already have inserted value at start of list) but as you can see Head is NULL in start it gives a error , Probably the error is in insert_at_end function . I think i am missing some concept of pointers