Insertion sorting & Deleting nodes in singly linked list

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

Here's the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <cstdlib>
#include <ctime>  
using namespace std;
class Node{
   public:
      int value;
      Node *next;
}; 
class LinkedList{ //Singly Linked List
public:
   LinkedList(){
   head=NULL;
   } 
   void SubMain(){
      bool result;   
      UniqueRandomData(10);
      CallInsertNode();
      PrintNode();
      result=CheckNode();
      CallDeleteNode(); 
   }
   void UniqueRandomData(int n){
      int i, j, k, temp;
      this->n=n;   
      x=new int[n]; 
   
   for(i=0; i<n; i++)
   x[i]=(i+1)*10; 
   
   for(i=0; i<n; i++){
      j=rand()%n;
      k=rand()%n;
      
      temp=x[j];
      x[j]=x[k];
      x[k]=temp;
   } 
   
   for(i=0; i<n; i++)
   cout<<x[i]<<" ";
   cout<<endl;
   }

   void InsertNode(int v){
      cout<<v<<" is inserted."<<endl;

      if (head == NULL) {
         head =  new Node();
       }
       else
       {
        head->next = new Node();
        head = head->next;
       }
            }
   }
   void PrintNode(){
      Node *cur=head; 
      while(cur!=0){
      cout<<cur->value<<" "; 
      cur=cur->next; 
      } 
   cout<<endl;
   }
   void CallInsertNode(){
      int i;
      for(i=0; i<n; i++)      
         InsertNode(x[i]);   
   }
   
   bool CheckNode(){
      Node *cur=head;
      while(cur !=NULL && cur->next !=NULL)
      if(cur->value > cur->next->value){
      cout<<"Error!"<<endl;
      cout<<cur->value<<", "<<cur->next->value<<endl;
      return false;
   }
   else 
   cur=cur->next;
   
   cout<<"Good job!"<<endl;
   return true;
   } 
   
   void CallDeleteNode(){
      int i;
   for(i=0; i<n; i++){  
      DeleteNode(x[i]); 
      this->PrintNode(); 
   }
   }

   void DeleteNode(int v){
       Node* prev = this->head;
        Node* current = this->head->next;
        while(current->value != v)
        {
                current = current->next;
                prev = prev->next;
        }
        if(current->value == v)
        {
                prev->next = current->next;
                delete current;
        }
      
      cout<<v<<" is deleted.";
      }
   
   private:
      Node *head;
      int *x;
      int n;
   
}; 
int main(){
   LinkedList a; 
   a.SubMain(); 
}
Last edited on
> 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.
Topic archived. No new replies allowed.