Linked lists

This is my code:

#include <iostream>

using namespace std;

struct node {
int data;
node* next;
};

void print (node* current)
{
while(current!= NULL)
{
cout<<current->data<<endl;
current = current->next;
}
}

node* add(node* head, node* new_node)
{
node* current = head;
node* previous = NULL;

while (current->next != NULL)
{
if (current->data >= new_node->data )
{
break;
}
else
{
previous = current;
current = current->next;
}


if (current == head)
{
new_node->next = head;
head = new_node;
}

else
{
new_node->next = current;
previous->next = new_node;
}

}

return head;
}


int main()
{
node n0;
n0.data = 3;
node* original;

node n1;
n1.data = 1;
n1.next = NULL;
original = add(&n1, &n0);
print(original);

cout<<endl;

node n2;
n2.data = 2;
n2.next = NULL;
n1.next = &n2;
original = add(&n1, &n0);
print(original);

cout<<endl;

node n3;
n3.data = 4;
n3.next = NULL;
n2.next = &n3;
original = add(&n1, &n0);
print(original);

cout<<endl;

node n4;
n4.data = 5;
n4.next = NULL;
n3.next = &n4;
original = add(&n1, &n0);
print(original);

cout<<endl;

return 0;
}




Im trying to add a node in a linked list.But or some reason it keeps on printing 3 and I can't figure out why
Topic archived. No new replies allowed.