Delete unsucessful.

Urm....i have a problem with the delete link part and i really do not know what's wrong with my codes...i am showing such a lengthy code just in case you guys need to really know what i am doing. If else, please scroll down to the bold part of the code for my problem (the codes cant really be bold tho =.="). I hope you guys can help me cause i am really in dead end and do not know what's wrong with my code.

My problem:
The code works fine but when i try to delete a link which doesnt exist, there will be a fatal error which prompts microsoft window's "error message" or rather, a pop up window which writes, "listfnb(name for this codes) has stopped working". What's wrong with my code actually?



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <conio.h>
using namespace std;

class link
{  
   public:
      link *previous;
      int data;
      link *next;
   
      link(): previous(NULL), next (NULL), data (0)
      {}
};

class list
{
   private:
           link *first;
           link *last;
           int count;
   
   public:
          list():first(NULL), last(NULL), count(0)
          {}
          
          void InsertFront(int d)
          {
               link *newlink = new link;
               newlink->data = d;
               newlink->next = first;
               newlink->previous = NULL;
               if(count>0)
                  first-> previous = newlink;
               else
                  last = newlink;
               
               first = newlink;
               count++;
          }
          
          void InsertBack(int d)
          {
               link *newlink = new link;
               newlink->data = d;
               newlink->next = NULL;
               newlink->previous = last;
               if(count>0)
                  last-> next = newlink;
               else
                  first = newlink;
               
               last =newlink;
               count++;
          }
          
          void countItem()
          {
               cout << "The total number of items are: " << count <<endl;
          }
          
          void DisplayFront()
          {
               link *current = first;
               cout << "First --> ";
               while(current!=NULL)
               {
               cout <<  current -> data << " --> ";
               current = current->next;
               }
               cout << "End\n";
          }
          
          void DisplayBack()
          {
               link *current = last;
               cout << "Last --> ";
               while(current!=NULL)
               {
               cout <<  current -> data << " --> ";
               current = current->previous;
               }
               cout << "End\n";
          }
          
          link *SearchValue(int n)
          {
               link *current = first;
               //link *current2 = last;
               while (current !=NULL)
               {
                     if ( n == current->data)
                     {
                     return current;
                     }
               current = current->next;
               }
               cout << "No value";
          }
          
          void DeleteValue (int n)
          {
               link *current = SearchValue(n);
               
               if (current == NULL)
               {
                  cout << "\nNo such value; delete unsuccessful";
               }
               
                else if (current->previous == NULL && current->next == NULL)
               {
                  delete current;
                  first = NULL;
                  last = NULL;
                  cout << "\nNo data left; link deleted";         
               }
               
               else if (current->previous == NULL)
               {
                  (current -> previous) -> next = current -> next;
               }
               
               else if (current->next == NULL)
               {
                  (current -> next) -> previous = current -> previous;
               }
               else
               
                  (current -> previous) -> next = current -> next;
                  delete current;
                  cout << "\n\nDelete Successful";
          }
          
};



int main()
{
    //list4.InsertFront(3);
    list4.DeleteValue(4);
    getch();
}

Last edited on
1. SearchValue() doesn't return a value if n is not found.

2.
1
2
3
4
5
6
7
8
9
10
11
else if (current->previous == NULL)
{
  //This line...
  (current -> previous) -> next = current -> next;
}

else if (current->next == NULL)
{
  //...and this line.
  (current -> next) -> previous = current -> previous;
}

This code invariably produces a segmentation fault, because you inverted those two lines. In fact, there's not even need to do all that derefencing:
1
2
3
4
5
6
7
8
9
10
else if (current->previous == NULL)
{
  current->next->previous=NULL;
}

else if (current->next == NULL)
{
  //...and this line.
  current->previous->next=NULL;
}

3. Your last case in DeleteValue() (when the link is in the middle of the list) is only updating current->previous->next, but not current->next->previous.
4. DeleteValue() is not decrementing count.
Oh! Thanks a lot! You rock, helios :D You're the SUN!!!
Topic archived. No new replies allowed.