Help with Linked List Delete

I can't figure out why when I want to delete the first node of my linked list, it doesn't seem to work. I think it might have something to do with the stressing of pointers in this class and that this one seems to be riddled with them, but I just can't figure it out. To me it looks like it should work. Kind of long, but just look first at my deleteNode function, then in main I am testing it. Thanks in advance for the help.

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251


#include <iostream>
//#include <conio.h>
using namespace std;


//prototypes here

//void traverseList(node *); // can't do prototype here

//class definition

class node
{

      //private data items anf functions

    // data items
    int* number;              // pointer to an int  
    node* next;               // node pointer  forward
    node* prev;               // node pointer  back
public:
    /*************************************
    *   public data items anf functions
    *************************************/ 
    // constructor
    node(int);
    // destructor
    ~node();  
  
    // setters
    void  setNext(node* node) {next   = node;}
    void  setPrev(node* node) {prev   = node;}
    void  setNumber(int x)    {*number = x;}
    
    // getters 
    node* getNext()           {return next;}
    node* getPrev()           {return prev;}
    int   getNumber()         {return *number;}

    // display the contents of a node
    void display (node* current);
};  //end class node

//member functions


//constructor

node::node(int num)
{
    cout << "in constructor for " << num << "\n";
    number = new int(num);
    setNext(NULL);
    setPrev(NULL);      
}//end constructor

  //destructor

node::~node()
{
    cout << "in destructor for " << getNumber() << "\n";
    delete number;
}//end destructor

/*****************************************
*    display the contents of a node
*****************************************/
void node::display(node* current)
{
    cout << "\n";
    cout << "number       " <<  getNumber() << "\n";
    cout << "prev node    " <<  getPrev()   << "\n";
    cout << "next node    " <<  getNext()   << "\n";
    cout << "current node " <<  current     << "\n";   
}//end node display

/*****************************************
*
*    step through the linked list
*    and print each node
*
*****************************************/
void traverseList(node* beginning)
{
     cout << "\n\nin traverseList\n";

     node* current;
     current = beginning;
     current->display(current); 
         
     while(current->getNext() != NULL)
     {
         current = current->getNext();
         current->display(current);
     }
}//ende traverseList

/*****************************************
*
*    delete a node from the linked list
*
*****************************************/
void deleteNode(node* beginning, int find)
{
     cout << "\n\nin delete node\n";  
     node* current;
     current = beginning;
         
     while(current->getNumber() != find && current->getNext() != NULL)
     {
         current = current->getNext();
     }
     current->display(current); 
     
     if(current->getNumber() != find)
     {
              cout << "\n no match \n\n";               
     }
     else
     {
         if(current->getPrev() == NULL)
         {
             cout << "\n delete beginning \n\n";
             current->getNext()->setPrev(NULL);            
         }   
         else if(current->getNext() == NULL)
         {
             cout << "\n delete end \n\n";  
             current->getPrev()->setNext(NULL); 
         }
         else
         {
             cout << "\n delete in nthe middle\n\n";
             current->getNext()->setPrev(current->getPrev());
             current->getPrev()->setNext(current->getNext());   
         }
         
     }

}//ende deleteNode

/*****************************************
*
*    insert a node into the linked list
*
*****************************************/
void insertNode(node* beginning, int find)
{
     cout << "\n\nin insert node\n";  

}//ende insert node


/*********************
*   other functions
*********************/ 
void loadLinkedList(node**);


/*********************
*   main
*********************/ 
int main()
{
    node  *start;                  // create a pointer to a node 
    node* *ppStart;                // create a node pointer to a node pointer
    ppStart = &start;              // put the address of our first node pointer
                                   // into the node pointer pointer
    
    loadLinkedList(ppStart);       // load the linked list       

    //traverseList(start);
    //system("pause");     
    
    
    deleteNode(start, 7);
    traverseList(start);
    system("pause");
    deleteNode(start, 2);
    traverseList(start);
    system("pause");
    deleteNode(start, 13);
    traverseList(start);
    system("pause");
    deleteNode(start, 8); 
    traverseList(start); 
    system("pause");   
    
    
    
    //traverseList(start);  

    cout << "\n";
    system("pause"); 
    return 0;
}

/**************************************************
*
*   create a linked list of seven nodes
*   with the values of 2, 3, 5, 7, 9, 11, and 13
*
**************************************************/
void loadLinkedList(node* *pps)
{   
    node* head;                    // start of the linked list
    node* temp;                    // a temporary place for a node pointer
    node* savePtr;                 // save the node pointer
    
    head = new node(2);            // create the 1st (start) node
    savePtr = head;                // save address of CURRENT node in savePtr
        
    temp = new node(3);            // create the next NEW node
    savePtr->setNext(temp);        // set setNext of the PREVIOUS node using address of CURRENT node
    temp->setPrev(savePtr);        // set setPrevious of CURRENT node using saved pointer
    savePtr = temp;                // save address of CURRENT node in savePtr to use with next node

    temp = new node(5);            // create the next NEW node
    savePtr->setNext(temp);        // set setNext of the PREVIOUS node using address of CURRENT node
    temp->setPrev(savePtr);        // set setPrevious of CURRENT node using saved pointer
    savePtr = temp;                // save address of CURRENT node in savePtr to use with next node
         
    temp = new node(7);            // create the next NEW node
    savePtr->setNext(temp);        // set setNext of the PREVIOUS node using address of CURRENT node
    temp->setPrev(savePtr);        // set setPrevious of CURRENT node using saved pointer
    savePtr = temp;                // save address of CURRENT node in savePtr to use with next node
    
    temp = new node(9);            // create the next NEW node
    savePtr->setNext(temp);        // set setNext of the PREVIOUS node using address of CURRENT node
    temp->setPrev(savePtr);        // set setPrevious of CURRENT node using saved pointer
    savePtr = temp;                // save address of CURRENT node in savePtr to use with next node
  
    temp = new node(11);           // create the next NEW node
    savePtr->setNext(temp);        // set setNext of the PREVIOUS node using address of CURRENT node
    temp->setPrev(savePtr);        // set setPrevious of CURRENT node using saved pointer
    savePtr = temp;                // save address of CURRENT node in savePtr to use with next node
        
    temp = new node(13);           // create the next NEW node
    savePtr->setNext(temp);        // set setNext of the PREVIOUS node using address of CURRENT node
    temp->setPrev(savePtr);        // set setPrevious of CURRENT node using saved pointer
    savePtr = temp;                // save address of CURRENT node in savePtr to use with next node
    
    traverseList(head);            // list the created linked list
    
    *pps = head;                   // pass the address of the 'start' node back to main()
    
    system("pause");
}
Last edited on
bump =)
I do not see where you are deleting the first node of your list.
bump:)
in main

deleteNode(start, 2);
traverseList(start);

after i try to delete the first one, 2, and i test it by traversing the list, it is still there everytime. All of the other deletions in main work, but that one doesnt ;;
I see that you call function deleteNode. However I do not see the code that deletes a node from the list.
If you think that this code snip

1
2
3
4
5
         if(current->getPrev() == NULL)
         {
             cout << "\n delete beginning \n\n";
             current->getNext()->setPrev(NULL);            
         }   


deletes the first node then you are wrong.
what should I do? I though that if I just set the second node's previous to null then i am essentially removing the first node from the list right? well, obviously not though haha, any ideas?
After the function call variable start has the same value as before the call.
So i see that I'm just passing a copy of the pointer right now. I cannot get it to get rid of two though. Tried changing:

deleteNode(&start, 2);
traverseList(start);

and:

void deleteNode(node** beginning, int find)
{
cout << "\n\nin delete node\n";
node* current;
current = *beginning;

but it still doesn't work ;;
Just got it; not sure how exactly, but here it is:

changed it back to:

---------------------------------------------------------------------------------

system("pause");
deleteNode(start, 2);

-----------------------------------------------------------------------------------

and then I did this:

--------------------------------------------------------------------------------------

void deleteNode(node* &beginning, int find)
{
cout << "\n\nin delete node\n";
node* current;
current = beginning;

while(current->getNumber() != find && current->getNext() != NULL)
{
current = current->getNext();
}
current->display(current);

if(current->getNumber() != find)
{
cout << "\n no match \n\n";
}
else
{
if(current->getPrev() == NULL)
{
cout << "\n delete beginning \n\n";
current->getNext()->setPrev(NULL);
*beginning = *current->getNext();
}

------------------------------------------------------------------------------

If anyone can explain why that worked, I'd love to hear it haha
Topic archived. No new replies allowed.