Inserting a node at the end of a linked list

Manvendra Singh
#include <stdio.h>
#include <conio.h>

struct node
{
    int data;
    struct node* next;
};

int main()
{
    struct node* head = NULL;
    struct node* second = NULL;
    struct node* third = NULL;

    head = (struct node*)malloc(sizeof(struct node));
    second = (struct node*)malloc(sizeof(struct node));
    third = (struct node*)malloc(sizeof(struct node));

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

    struct node* new1;
    struct node* temp1;

    temp1 = head;

    while(temp1->next != NULL)
    {
        temp1 = temp1->next;
    }

    new1 = (struct node*)malloc(sizeof(struct node));

    temp1->next = new1;
    new1->data = 5;
    new1->next = NULL;

    while(temp1 != NULL)
    {
        printf("%d ",temp1->data);
        temp1 = temp1->next;
    }

    return 0;
}

This is the program for inserting a node at the end of linked list. The expected output is = 1 2 3 5, 5 is the new node's value here. But the current output is = 3 5. I don't know where am I wrong. Any answer will be appreciated.

Gopi
while(temp1->next != NULL)
 {
    temp1 = temp1->next;
 }

After this loop your temp1 is at the end of the list and you are adding a node at the end of the list.

Now you are trying to print from temp1 obviously you will get only 2 nodes new one and the one before it. If you want the whole list print from head. Just before printing after adding your new node. Point temp1 to head.

temp1 = head;
while(temp1 != NULL)
{
    printf("%d ",temp1->data);
    temp1 = temp1->next;
}

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 a node at the end of linked list

From Dev

Inserting a node in the end of a linked list

From Dev

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

From Dev

Inserting a node at the end of a doubly linked list

From Dev

inserting a node at the end of linked list recursivly

From Dev

Inserting linked list at the end

From Dev

Inserting Node into a Linked List

From Dev

Unexpected value after inserting node at the end of linked list

From Dev

inserting node to linked list in c

From Dev

Inserting Node in a Sorted linked list

From Dev

inserting node at beginning of linked list

From Dev

inserting node at beginning of linked list

From Dev

Adding a node to the end of a linked list

From Dev

Linked list, add node to end

From Dev

Add end node to linked list

From Dev

Inserting a node in a pre sorted linked list

From Dev

Inserting a node into a sorted doubly linked list

From Dev

Singly linked list in C(Inserting a node)

From Dev

Linked list - Inserting in middle, linking new node

From Dev

Inserting a node into a sorted doubly linked list

From Dev

Inserting a node at a specific position in a Linked list

From Dev

Inserting node at beginning of singly linked list

From Dev

Inserting a node before a given node in doubly linked list

From Dev

Inserting a new node into a doubly linked list after a given node

From Dev

adding a node at the end of a Linked List (java)

From Dev

Deletion of node at end of linked list C++

From Dev

Removing the end node from a linked list

From Dev

linked list moving node to the end C

From Dev

adding a node at the end of a Linked List (java)

Related Related

HotTag

Archive