So the whole concept is to sort a singly linked list using insertion sort and deleting the nodes in order. The other codes are given, so all I have to do is fill in the "void InsertNode(int v)" and "void DeleteNode(int v)" and I am having trouble with it.
The result should be something like:
50 30 20 10 70 80 60 90 100 40
50 is inserted
30 is inserted
20 is inserted
...
100 is inserted
10 20 30 40 50 60 70 80 90 100
Good Job!
50 is deleted
10 20 30 40 60 70 80 90 100
30 is deleted
10 20 40 60 70 80 90 100
20 is deleted
10 40 60 70 80 90 100
10 is deleted
40 60 70 80 90 100
70 is deleted
40 60 80 90 100
80 is deleted
40 60 90 100
60 is deleted
40 90 100
90 is deleted
40 100
100 is deleted
40
40 is deleted
But mine would be like:
50 30 20 10 70 80 60 90 100 40
50 is inserted
30 is inserted
20 is inserted
...
100 is inserted
0
Good Job!
0
0
0
0
0
0
0
0
0
0
> But mine would be like:
funny, your code doesn't compile.
1 2 3 4 5 6 7 8 9 10 11 12 13
void InsertNode(int v){
cout<<v<<" is inserted."<<endl;
if (head == NULL) {
head = new Node();
}
else
{
head->next = new Node();
head = head->next;
}
} //¿?
}
Let's check your algorithm.
If the list is empty, you'll create a new node and make `head' point to it
(h)a -> *
If you try to insert another element, you'll create a new node, make head->next point to it, and update head
(h)a->b->*
a->(h)b->*
¿how do you expect to access `a' now?
By the way, when creating nodes you are not initializing its members.