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