Thread: Function to add node in list

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    28

    Function to add node in list

    I'm going to make list in which I have two pointer's head and tail. I understand that head always point to first node in list and tail always point to last node in the list. When new node is added in the list, head will remain the same, whereas the tail points to new node added


    Code:
    #include<stdio.h>
    #include<stdlib.h> 
        
    struct Node  
    { 
      int Value;              
      struct Node *Next;      
    }; 
    
    
    int main() 
    { 
      struct Node* Head = NULL;
      struct Node* Tail = NULL; 
      
      return 0; 
    }
    I don't understand how to create function that add the new node in the end of list. What argument has to be passed in the function so that new node should be added to end of list. will the function return any value or it will be void

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you're dealing with a linked list that has both head and tail, then I would suggest something like this:
    Code:
    struct Node
    {
        int Value;
        struct Node *Next;
    };
    
    struct LinkedList
    {
        struct Node *Head;
        struct Node *Tail;
    };
    
    void appendNode(struct LinkedList *List, int value)
    {
        /* ... */
    }
    
    int main(void)
    {
        struct LinkedList List = {NULL, NULL};
        appendNode(&List, 123);
        /* destroy the linked list when done
           ... */
        return 0;
    }
    The reason for struct LinkedList is that sometimes the linked list operation might change the head, other times it might change the tail, so generally you have to pass both (and when you do expect either to change rather than merely what they point to to change, the parameter will have to be a pointer to a pointer). This thus simplifies the process by introducing another level of abstraction.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2020
    Posts
    28
    Quote Originally Posted by laserlight View Post
    If you're dealing with a linked list that has both head and tail, then I would suggest something like this:
    would you please help me to explore function.

    List is a structure variable in which you are assigning null value twice but I have never seen this type of initialize
    Code:
    struct LinkedList List = {NULL, NULL};
    you are passing pointer to structure type struct and value in function. What happens in function.

    Code:
    void appendNode(struct LinkedList *List, int value)
    {
        /* ... */
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Other function, Node at the end of a List
    By letthem in forum C Programming
    Replies: 4
    Last Post: 06-25-2019, 02:15 PM
  2. Function which insert one Node in a List
    By letthem in forum C Programming
    Replies: 1
    Last Post: 06-24-2019, 03:00 PM
  3. Replies: 6
    Last Post: 04-25-2013, 01:50 AM
  4. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM

Tags for this Thread