Problem with linked list "delete"

Hi everybody,
I am writing my first linked list , where till now I have done insert and print are working properly, delete is giving little problem.there are 3 cases .
1) if ther's only one node in the list and if we delete it then it gets deleted without any error but if I try to print after that it goes in an infinite loop.
2)suppose there are three nodes present and if the user deletes the first node of the list , it works fine and when I print after this it print also correct ,
3) If i want to delete some middle node like 2nd node, the output screen closes by itself
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
  void link_list :: print_list()
{
     if (head==NULL)
     {
                    cout<< "Empty list"<< endl;
                  
     }
       
   else
  {
    int count = 0;
    list=head;
    while (list != NULL)
    {          
      count++;
      cout << "Node    " << "value " << endl;
      cout << count << "     " << list->num << endl;
      list = list->next;
    }
  }
}

void link_list :: del_pos()
  { //1
        if(head==NULL)
         { //2
                    cout<<"List is empty can't delete"<<endl;
         } //2
        else
          { //3
            char ch;
          do
             { //4
                     node *s ;
                 int pos ;
                 int count =1;
                  s=head;
                 list=head;
               cout<<"enter the node you wish to delete"<<endl;
               cin >> pos;
               if(pos==1 && head->next==NULL)
                 { // 5
                   cout<<"u made the list empty !!!"<<endl;;
                 } //5
               else 
                 { //6
                    if(pos==1)
                      { //7
                        //s=head;
                        head=list->next;   
                      } //7
                    else
                      { //8
                        //s=list->next;
                       while(list!=NULL)
                        { //9
                         list=list->next;
                         s=list->next->next;
                         count++;
                         
                    if(count==pos)
                      { //10
                       list->next = s->next;
                       break;
                       } //10
                       }//9
                      } //8
                   } //6
               delete s;
         /* list=head;
          while(list->next->!=NULL)
          {
            list=list->next;
            }
            list->next=NULL;*/
            
            
            cout <<"do u wish to delete another node?"<<endl;
            cin >> ch;
            
            } while(ch=='y'||ch=='Y'); //4
         } //3      
    
 }//1
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
#include <stdexcept>
#include <string>

void link_list :: del_pos() {
    char resp;
    
    do {
        if (this->size() /*make a method called size*/ == 0) {
            std::cout << "List is empty\n";
            break;
        }
        
        std::cout << "Enter a position to delete: ";
        unsigned pos;
        std::cin >> pos;
        if (pos < 1 || pos > this->size() /*make a method called size*/) {
            throw std::out_of_range("The value entered must be"
                                " between 1 and " + to_string(this->size()));
        }
    
        for (node *currpos = head, *prev = nullptr; --pos; prev = currpos, currpos = currpos->next)
            ;
    
        if (prev == nullptr)
            head = currpos->next;
        else
            prev->next = currpos->next;
        
        delete currpos;
        
        /*decrement size here*/
        
        std::cout << "Do you wish to delete another node? ";
        std::cin >> resp;
        
    } while (resp == 'Y' || resp == 'y');
}
Last edited on
thanks a lot for your reply , but at the same is it also possible to modify my code so that it runs perfectly ?
I have resoled the first problem by adding head=NULL; but still error 3 is there :( dont know how to do it, please help me.
Someone please tell me me how to resolve the 3rd issue
Last edited on
Where you have "head=NULL", Smac89 has lines 24-25.
Where you have "delete from middle or end", Smac89 has line 27.
These are tied to the "find the victim" logic of line 21.

Can you visualize what Smac89's code does?
I am sorry but I am not able to get my mistake
1
2
3
4
5
6
7
8
9
10
11
s = head; // s -> 0
list = head; // list -> 0

while ( list != NULL )
{
  list = list->next; // list -> 1 (or NULL)
  s = list->next->next; // s -> 3 (or NULL->next or NULL->next->next)

  if (count==pos) // assume true
  {
    list->next = s->next; // 1.next == 4, i.e. positions 2 and 3 are forgotten 

I have modified the code a little bit , still getting the same error , console is stopping
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
else 
                 { //6
                    if(pos==1)
                      { //7
                        //s=head;
                        head=list->next;   
                      } //7
                    else
                      { //8
                        //s=list->next;
                       while(list!=NULL)
                        { //9
                         list=list->next;
                         //s=list->next->next;
                        
                         count++;
                             
                         t=s;
                         s=list->next;
                          }//9
                    if(count==pos)
                      { //10
                       t->next = s->next;
                     
                       } //10
                  
                      } //8
                   } //6
               delete s;
                
            cout <<"do u wish to delete another node?"<<endl;
            cin >> ch;
            
            } while(ch=='y'||ch=='Y'); //4
         } //3      
    
 }//1 
even this is not working, what is wrong , someone please tell.
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
else 
                 { //6
                    if(pos==1)
                      { //7
                        //s=head;
                        head=list->next;   
                      } //7
                    else
                      { //8
                        //s=list->next;
                       while(list!=NULL)
                        { //9
                         list=list->next;
                         //s=list->next->next;
                        
                         count++;
                         }
                         if (count != pos)
                         {
                               t=list;
                               list=list->next;
                         }   
                         else
                         {
                               t->next = list->next;
                               }
                               
                         /*t=s;
                         s=list->next;
                          }//9
                    if(count==pos)
                      { //10
                       t->next = s->next;
                     
                       } //10*/
                  
                      } //8
                   } //6
               delete s;
                    
            cout <<"do u wish to delete another node?"<<endl;
            cin >> ch;
            
            } while(ch=='y'||ch=='Y'); //4
         } //3      
    
 }//1 
Your indentation is not very informative (at least on the post).

Lets try something else for a moment. A simple excercise. Rewrite this Smac89's for-loop as a while-loop:
1
2
for (node *currpos = head, *prev = nullptr; --pos; prev = currpos, currpos = currpos->next)
  ;
Topic archived. No new replies allowed.