Inserting node at beginning of singly linked list

ibrahim abdalluh

I have a problem with my add_to_list function here.

I'm using this function to add a node to the begging of the singly linked list that referenced by list pointer.

The problem is: only the first node is added, then if I add any more, I lose the trace of the list.

#include <stdio.h>
#include <stdlib.h>

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

struct node *add_to_list(struct node *list , int n){
     struct node *new_node ;
     new_node = malloc( sizeof(struct node) ); //create new node
     if(new_node == NULL){
         printf("Error ,malloc failed to allocate memory\n");
         exit(EXIT_FAILURE);
     }
     new_node->value = n; //initiate value field
     new_node->next = list;
     return new_node;
}

int main(){
    struct node * first = NULL;
    struct node * temp = first;
    first = add_to_list(first,10);
    if(first != NULL)
        printf("node added\n");
    else
        printf("add failed\n");
    first = add_to_list(first,20);
    if(first == NULL)
        printf("node added\n");
    else
        printf("add failed\n");
    first = add_to_list(first,30);
    if(first == NULL)
        printf("node added\n");
    else
        printf("add failed\n");

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

    return 0;
}
Chris Turner

So at the beginning of main you've got these two lines...

struct node * first = NULL;
struct node * temp = first;

...which assign NULL to first and then the value of first to temp which means both of them are NULL. This is a one time assignment - temp won't get updated as first changes.

When you get to the bottom of the function you have this loop, but nothing has updated the value of temp since it was first assigned NULL.

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

The solution is to assign the current value of first to temp just before the loop like so:

temp = first;
while(temp!=NULL){
    printf("%d-->",(temp->value));
    temp = temp ->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 node at beginning of linked list

From Dev

inserting node at beginning of linked list

From Dev

Move node to beginning of singly linked list

From Dev

Singly linked list in C(Inserting a node)

From Dev

Inserting Node into a Linked List

From Dev

Error inserting string at the beginning of a Linked List

From Dev

inserting node to linked list in c

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

From Dev

Remove node from Singly Linked List

From Dev

Create new Node for Singly Linked List in Java

From Dev

Removing a node from a singly linked list

From Dev

Deleting a node in a singly linked list in java

From Dev

singly linked list error while adding a node

From Dev

Singly linked list deleting the last node

From Dev

Finding a node in singly linked list time complexity

From Dev

How to add a node at the beginning of a single linked list?

From Dev

Insert a node before another node in singly linked list in Python?

From Dev

Inserting a node in a pre sorted linked list

From Dev

Inserting a node into a sorted doubly 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

Inserting a node at the end of a doubly linked list

From Dev

Inserting a node into a sorted doubly linked list

From Dev

inserting a node at the end of linked list recursivly

From Dev

Inserting a node at a specific position in a Linked list

From Dev

Implementing Singly Linked List

Related Related

HotTag

Archive