This function
SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {
SinglyLinkedListNode* temp=head;
while(temp!=NULL){
temp=temp->next;
}
SinglyLinkedListNode* temp1;
temp1->data=data;
temp1->next=NULL;
temp->next=temp1;
return temp;
}
does not make sense. After this loop
while(temp!=NULL){
temp=temp->next;
}
the pointer temp is equal to NULL. So this statement
temp->next=temp1;
invokes undefined behavior.
The pointer temp1 was not initialized. So again these statements
temp1->data=data;
temp1->next=NULL;
invoke undefined behavior.
The user of the function does not know whether the returned pointer is the head pointer or the last pointer of the list. So it is unclear whether to assign the returned pointer to the head pointer or just to ignore the returned value.
The function can look the following way.
void insertNodeAtTail( SinglyLinkedListNode * &head, int data )
{
SinglyLinkedListNode **current = &head;
while ( *current != nullptr ) current = &( *current )->next;
*current = new SinglyLinkedListNode { data, nullptr };
}
If in main you defined the pointer to the head node like
SinglyLinkedListNode *head = nullptr;
then a function call will look like
insertNodeAtTail( head, some_data );
Another definition of the function can look the following way
SinglyLinkedListNode* insertNodeAtTail( SinglyLinkedListNode *head, int data )
{
SinglyLinkedListNode *new_node = new SinglyLinkedListNode { data, nullptr };
if ( head == nullptr )
{
head = new_node;
}
else
{
SinglyLinkedListNode *current = head;
while ( current->next != nullptr ) current = current->next;
current->next = new_node;
}
return head;
}
In this case if in main you defined the pointer to the head node like
SinglyLinkedListNode *head = nullptr;
then the function call will look like
head = insertNodeAtTail( head, some_data );
Between these two function definitions the first function definition is preferable because there is no need to remember to assign the returned pointer to the head node.
Bear in mind that if you have a singly-linked list and want to append new nodes to the tail of the list ten it is better to define two-sided singly-linked list. In this case the list definition can look like
class SinglyLinkedList
{
private:
struct Node
{
int data,
Node *next;
} *head = nullptr, *tail = nullptr;
public:
SinglyLinkedList() = default;
void insertNodeAtHead( int data );
void insertNodeAtTail( int data );
// other member functions;
};
SinglyLinkedListNode* temp1; temp1->data=data;does not work, where doestemp1point to?tempafter thewhile(temp!=NULL)loop?