Hwabum Kim
Hwabum Kim

Reputation: 1

C++ 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.

#include <iostream>

using namespace 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();
}

Upvotes: 0

Views: 3422

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

Your code does not make great sense. For example function addNode should have a parameter for the value that is added to the list. Also there is no need to have two functions addNode and insertNode because they do the same (at least in your functions' implementations). If you want to have some function insertNode then it should have a different semantic.

Here is a demonstrative program that shows how function addNode can be written. You can use it as a template for your program. In this demonstrative program function displayList is also recursive. Of course you need to write also the function that will delete all nodes. It also can be recursive.

#include <iostream>

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

linkedlist * addNode( linkedlist *head, int value )
{
    return head == nullptr 
           ? ( new linkedlist { value, nullptr } )
           : ( head->next = addNode( head->next, value ), head );
}

void displayList( linkedlist *head )
{
    if ( head == nullptr )
    {
        std::cout << std::endl;
    }
    else
    {
        std::cout << head->value << ' ';
        displayList( head->next );
    }
}

int main()
{
    linkedlist *head = nullptr;

    for ( int x : { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ) head = addNode( head, x );
    displayList( head );
}

The program output is

0 1 2 3 4 5 6 7 8 9

Upvotes: 1

TartanLlama
TartanLlama

Reputation: 65610

You are trying to modify the pointer you pass in to addNode, but the pointer is passed by-value, so you won't see the modification at the call site.

To fix this, take in the pointer by reference:

linkedlist *addNode(linkedlist*& temp)

Upvotes: 3

Related Questions