In the linked list when we perform insertLast(int item) function, we do the following steps:
struct node *temp;
struct node *newItem;
newItem = (struct node*)malloc(sizeof(struct node));
temp = head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newItem;
newItem->next = NULL;
But if we do:
struct node *temp;
struct node *newItem;
newItem = (struct node*)malloc(sizeof(struct node));
temp = head;
while(temp != NULL){
temp = temp->next;
}
temp = newItem;
newItem->next = NULL;
we get an error, why does this happen?