Hello so i'm something new to linked lists and data structrs generally so i want to creat two functions one initiallise the doubly linked list and the other one print it but when i compile it it doesnt print anything where did i miss exactly (i heard that i should use the debugger but i didnt understand how to use it on Dev c++ IDE)
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node
{ int data;
Node *next;
Node *previous;
};
Node *head,*end;
Node* insertion()
{
Node *first_node =new Node;
if (head==NULL || first_node==NULL)
exit(EXIT_FAILURE);
first_node->data=10;
first_node->previous=NULL;
first_node->next=NULL;
head=first_node;
end=first_node;
return head;
}
void affiche()
{
Node *current;
current=head;
if(head==NULL)
exit(EXIT_FAILURE);
while(current->next!=NULL)
{
cout<<current->data <<" ";
current=current->next;
}
}
int main()
{
Node *MyList=insertion();
affiche();
return 0;
}
Just change
while(current->next!=NULL)
to
while(current!=NULL)
Your version stops printing the list too early.
(But please learn how to use a debugger, you would have found this error very quickly with a debugger).
EDIT
You also need to remove this
if (head==NULL || first_node==NULL)
exit(EXIT_FAILURE);
head==NULL
is initially true and first_node==NULL
is never true. (new
never returns NULL). Since head==NULL
is initially true your program is always going to quit at the point.
EDIT(2)
And remove this
if(head==NULL)
exit(EXIT_FAILURE);
Why do you think printing an empty list is an error that means you have to exit the program? It's perfectly OK to print an empty list.