This is how i add elements to the head of a linked list
//typedef void* VoidPtr //typedef node* NodePtr struct Node { NodePtr next VoidPtr data }; void LinkedList::Addelement(VoidPtr horoscope) { if(head == NULL) { head = new Node; head->data = horoscope; head->next = NULL; } NodePtr temp = new Node; temp -> data = horoscope; temp -> next = head; head = temp; } This is how i add elements to the tail of a linked list
void LinkedList::addelementfromback(VoidPtr horoscope) { NodePtr temp=head; if(head == NULL) { head = new Node; head->data = horoscope; head->next = NULL; } while( temp->next != NULL) { temp=temp->next } NodePtr element=New Node; element->data=horoscope; element->next=NULL; temp->next=element; } i dont understand why we use temp=element for adding to the head of a linked list but for adding to the tail of a linked list we use temp->next=element. I dont understand why we cant use while temp=next for adding element to tail of linked list
while (temp != NULL)for adding to the head of a linked listtemp != NULL, the loop breaks whentempis equal toNULL. How would you get back fromNULLto the last list node?std::list. It's already been thoroughly tested.