Inserting linked list at the end

Mani Theja

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;
}
praneet drolia

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

inserting node to linked list in c

From Dev

C - Inserting into linked list in ascending order

From Dev

inserting node at beginning of linked list

From Dev

Inserting Node in a Sorted linked list

From Dev

Inserting a node at the end of linked list

From Dev

Inserting a node at the end of a linked list

From Dev

Inserting an element into an already sorted linked list

From Dev

Inserting a node into a sorted doubly linked list

From Dev

Inserting object in ascending order in Linked List

From Dev

inserting string in stack using linked list

From Dev

Singly linked list in C(Inserting a node)

From Dev

Inserting a node in a pre sorted linked list

From Dev

Linked list - Inserting in middle, linking new node

From Dev

Inserting a node at the end of linked list, .exe crashes

From Dev

Sorted doubly linked list and inserting values

From Dev

Inserting Into Linked List Python

From Dev

Getting frequency count while inserting in linked list

From Dev

Detecting the end of a linked list

From Dev

Inserting Node into a Linked List

From Dev

inserting node at beginning of linked list

From Dev

Inserting a node at the end of a doubly linked list

From Dev

Insertion at end of linked list

From Dev

Inserting a node into a sorted doubly linked list

From Dev

Unexpected value after inserting node at the end of linked list

From Dev

Check the end of the linked list

From Dev

inserting a node at the end of linked list recursivly

From Dev

Error inserting string at the beginning of a Linked List

From Dev

Inserting a node in the end of a linked list

From Dev

Linked list inserting with two constraints