here is my linkedlistcode for inserting node at the end. i am getting the error code dumped. so please help me what is the wrong with the code.
if I keep return; at the end and at the line after *headRef = newnode; its working. so why to return for a void function.
struct node
{
int data; // node format
struct node* next;
};
void insertAtEnd(struct node** headRef, int newData)
{
struct node* ptr;
struct node* newnode;
ptr = (*headRef);
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = newData;
newnode->next = NULL;
if (*headRef == NULL)
{
*headRef = newnode;
}
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = newnode;
}
you should have to return inside if because after if statement it again comes into while loop and add extra node at the end
struct node
{
int data; // node format
struct node* next;
};
void insertAtEnd(struct node** headRef, int newData)
{
struct node* ptr;
struct node* newnode;
ptr = (*headRef);
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = newData;
newnode->next = NULL;
if (*headRef == NULL)
{
*headRef = newnode;
return;
}
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = newnode;
return;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments