Thread: How to link next node in linked list

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    72

    How to link next node in linked list

    I am looking help to change function in my code. currently function create node, assign data to node but it doesn't link node to next node in list

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    
    
    struct node 
    {
        int data;
        struct node *nextNode;
    };
    
    
    void addNode(int value )
    {
        struct node *newNode = NULL;
        
        newNode = malloc (sizeof(*newNode)); 
        
        if (newNode != NULL)
        {
            newNode -> data = value;
            
            newNode -> nextNode = NULL;
        }
    }
    
    
    int main() 
    {
    
    
        addNode( 2 );
        
        return 0;
    }
    I know This line doesn't point to address of next node

    Code:
     newNode -> nextNode = NULL;
    can anyone explain theoritically what I need to link next node in linked list ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,119
    Well you need the idea of a current list you want to add a node to.

    Normally I would write something like
    list = addNode(list, 2);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    72
    Quote Originally Posted by Salem View Post
    Well you need the idea of a current list you want to add a node to.
    That's what I am trying to understand

    Quote Originally Posted by Salem View Post
    Normally I would write something like
    list = addNode(list, 2);
    I think When you call a function, you pass pointer 'list' and value as a argument.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,119
    Yes, that's right.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2022
    Posts
    72
    Quote Originally Posted by Salem View Post
    Yes, that's right.
    What happens by doing this ?

    Where does new node add in list ? at beginning of list or end of list ?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,119
    > Where does new node add in list ? at beginning of list or end of list ?
    You can do either.
    Or you can splice the node into the middle of the list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Feb 2022
    Posts
    72
    Quote Originally Posted by Salem View Post
    > Where does new node add in list ? at beginning of list or end of list ?
    You can do either.
    Or you can splice the node into the middle of the list.
    I want to add node at end of list. I think I have created one node of list

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node 
    {
        int data;
        struct node *nextNode;
    };
    
    
    
    
    void addNode( struct node *list, int value )
    {
        struct node *newNode = NULL;
        
        newNode = malloc (sizeof(*newNode)); 
        
        if (newNode != NULL)
        {
            newNode -> data = value;
            
            newNode -> nextNode = list;
        }
    }
    
    
    
    
    int main() 
    {
    
    
        struct node *list = NULL;
        list = malloc (sizeof(*list)); 
        
        if (list != NULL)
        {
             addNode(list,  2 );
        }
        
       
        
        return 0;
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,119
    Code:
    struct node *addNodeToFront( struct node *list, int value )
    {
        struct node *newNode = malloc (sizeof(*newNode)); 
        if (newNode != NULL)
        {
            newNode -> data = value;
            newNode -> nextNode = list;
        }
        return newNode;
    }
    
    struct node *addNodeToBack( struct node *list, int value )
    {
        struct node *newNode = malloc (sizeof(*newNode)); 
        if (newNode != NULL)
        {
            newNode -> data = value;
            newNode -> nextNode = NULL;
            if ( list == NULL )
            {
                list = newNode;
            }
            else
            {
                struct node *tail = head;
                while ( tail->next != NULL ) tail = tail->next;
                tail->next = newNode;
        }
        return list;
    }
     
    int main() 
    {
        struct node *list = NULL;
        list = addNodeToFront(list,  2 );    
        list = addNodeToBack(list,  3 );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-12-2011, 03:41 AM
  2. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  3. traversing a linked list with a node and list class
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 11:57 AM
  4. Link list and node problem
    By tom_mk in forum C++ Programming
    Replies: 0
    Last Post: 02-21-2002, 05:18 PM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM

Tags for this Thread