Thread: Creating a linked list using recursive function

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    1

    Question Creating a linked list using recursive function

    Hi I am a beginner learning c++ I am trying to create a linked list using recursive function I though I got more or less pointer, linked list, data structure etc but I am stuck for 2 days.
    Here's my entire code.
    What I am basically trying to do is like I said creating a linked list using only recursive function. The problem is my pointer variable 'head' is always pointing to NULL and I can't figure out why and i guess i misunderstood a lot of things but i simply don't know what ... and more i try more i am getting confused.
    It must be quite newbie question but Id really appreciate if someone can help me.


    Code:
     
    
    #include
    Code:
    <iostream>
    
    usingnamespace std;
    
    struct linkedlist
    {
        int value;
        linkedlist *next;
    };
    
    void insertNode(linkedlist* temp)
    {
        if(temp->next != NULL)
        {
            insertNode(temp->next);
        }
        else
        {
            temp->next =new linkedlist;
            temp->next->value =0;
            temp->next->next = NULL;
        }
    
    }
    
    
    linkedlist *addNode(linkedlist *temp)
    { 
        if(temp == NULL)
        {
            linkedlist *newelement =new linkedlist;
            newelement->value =0;
            newelement->next = NULL;
            temp = newelement;
    
            return newelement;
        }
        else
        {
            insertNode(temp);
        }
    }
    
    void displaylist(linkedlist *temp)
    {
        while(temp != NULL)
        {
            cout << temp->value << endl;
            temp = temp->next;
        }
    }
    
    int main()
    {
        linkedlist *head = NULL;
    
        linkedlist *element1 = addNode(head);
        linkedlist *element2 = addNode(head);
        linkedlist *element3 = addNode(head);
        linkedlist *element4 = addNode(head);
        linkedlist *element5 = addNode(head);
    
        cin.ignore();
        cin.get();
    }
    
    

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I am not a C++ programmer, but since your code seems pretty "C-like", I'll just point out the problem I see.

    You pass the "head" pointer to "addnode()" and try to update it, but any changes are local only to that function (i.e. not seen in "main()"). In order to update this variable in the calling function, you need to pass a pointer to that variable (in this case, a pointer to a pointer).

    There are other issues in the code (e.g. creating discrete variables "element 1" etc defeats the purpose of a linked list), but hopefully this should get you set on the right path.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    If you want another nice linked list exercise try radioactive mutant vampire bunnies.Beginner Exercises - C++ Forum

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive printing of Linked List.
    By thebenman in forum C++ Programming
    Replies: 6
    Last Post: 06-20-2015, 02:24 AM
  2. Problem Creating Linked List in Function
    By Stunner in forum C++ Programming
    Replies: 21
    Last Post: 07-25-2007, 02:56 AM
  3. splitting linked list recursive vs. iterative
    By Micko in forum C Programming
    Replies: 7
    Last Post: 03-17-2005, 05:51 PM
  4. linked list recursive help
    By dreamkk in forum C Programming
    Replies: 3
    Last Post: 10-11-2003, 12:11 AM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM

Tags for this Thread