My linkedListPRogram does not delete the node

The program compiles and runs fine but I cannot delete any node.
Here is the header file:
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
template<class Type>
struct nodeType
{
        Type info;
        nodeType<Type> *link;
};
#include<iostream>
#include<cassert>

using namespace std;

template <class Type>
class linkedListType
{
public:
const linkedListType<Type>& operator=
(const linkedListType<Type>&);
//Overload the assignment operator.
void initializeList();
//Initialize the list to an empty state.
//Postcondition: first = NULL, last = NULL, count = 0;
bool isEmptyList() const;
//Function to determine whether the list is empty.
//Postcondition: Returns true if the list is empty, otherwise
// it returns false.
void print() const;
//Function to output the data contained in each node.
//Postcondition: none
int length() const;
//Function to return the number of nodes in the list.
//Postcondition: The value of count is returned.
void destroyList();
//Function to delete all the nodes from the list.
//Postcondition: first = NULL, last = NULL, count = 0;
Type front() const;
//Function to return the first element of the list.
//Precondition: The list must exist and must not be empty.
//Postcondition: If the list is empty, the program terminates;
// otherwise, the first element of the list is returned.
Type back() const;
//Function to return the last element of the list.
//Precondition: The list must exist and must not be empty.
//Postcondition: If the list is empty, the program
// terminates; otherwise, the last
// element of the list is returned.
bool search(const Type& searchItem) const;
//Function to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is in the list,
// otherwise the value false is returned.
void insertFirst(const Type& newItem);
//Function to insert newItem at the beginning of the list.
//Postcondition: first points to the new list, newItem is
// inserted at the beginning of the list, last points to
// the last node in the list, and count is incremented by
// 1.
void insertLast(const Type& newItem);
//Function to insert newItem at the end of the list.
//Postcondition: first points to the new list, newItem is
// inserted at the end of the list, last points to the
// last node in the list, and count is incremented by 1.
void deleteNode(const Type& deleteItem);
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing deleteItem is
// deleted from the list. first points to the first node,
// last points to the last node of the updated list, and
// count is decremented by 1.
linkedListType();
//default constructor
//Initializes the list to an empty state.
//Postcondition: first = NULL, last = NULL, count = 0;
linkedListType(const linkedListType<Type>& otherList);
//copy constructor
~linkedListType();
//destructor
//Deletes all the nodes from the list.
//Postcondition: The list object is destroyed.
protected:
int count; //variable to store the number of list elements
nodeType<Type> *first; //pointer to the first node of the list
nodeType<Type> *last; //pointer to the last node of the list
private:
void copyList(const linkedListType<Type>& otherList);
//Function to make a copy of otherList.
//Postcondition: A copy of otherList is created and assigned
// to this list.
};

int main() //Line 4
{
//orderedLinkedList<int> list1, list2; //Line 5
linkedListType<int> list1, list2;     //Line 5
int num; //Line 6
cout << "Line 7: Enter numbers ending "
<< "with -999." << endl; //Line 7
cin >> num; //Line 8
while (num != -999) //Line 9
{ //Line 10
//list1.insert(num); //Line 11
list1.insertFirst(num);
cin >> num; //Line 12
} //Line 13
cout << endl; //Line 14
cout << "Line 15: list1: "; //Line 15
list1.print(); //Line 16
cout << endl; //Line 17
list2 = list1; //test the assignment operator Line 18
cout << "Line 19: list2: "; //Line 19
list2.print(); //Line 20
cout << endl; //Line 21
cout << "Line 22: Enter the number to be "
<< "deleted: "; //Line 22
cin >> num; //Line 23
cout << endl; //Line 24
//list2.deleteNode(num); //Line 25
cout << "Line 26: After deleting "
<< num << ", list2: " << endl; //Line 26
list2.print(); //Line 27
cout << endl; //Line 28
return 0; //Line 29
}

Here is the implementation:
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
template <class Type>
bool linkedListType<Type>::isEmptyList() const
{
return (first == NULL);
}

template <class Type>
linkedListType<Type>::linkedListType() //default constructor
{
first = NULL;
last = NULL;
count = 0;
}

template <class Type>
void linkedListType<Type>::destroyList()
{
nodeType<Type> *temp; //pointer to deallocate the memory
//occupied by the node
while (first != NULL) //while there are nodes in the list
{
temp = first; //set temp to the current node
first = first->link; //advance first to the next node
delete temp; //deallocate the memory occupied by temp
}
last = NULL; //initialize last to NULL; first has already
//been set to NULL by the while loop
count = 0;
}
template <class Type>
void linkedListType<Type>::initializeList()
{
destroyList(); //if the list has any nodes, delete them
}

template <class Type>
void linkedListType<Type>::print() const
{
nodeType<Type> *current; //pointer to traverse the list
current = first; //set current point to the first node
while (current != NULL) //while more data to print
{
cout << current->info << " ";
current = current->link;
}
}//end print

template <class Type>
int linkedListType<Type>::length() const
{
return count;
}

template <class Type>
Type linkedListType<Type>::front() const
{
assert(first != NULL);
return first->info; //return the info of the first node
}//end front

template <class Type>
Type linkedListType<Type>::back() const
{
assert(last != NULL);
return last->info; //return the info of the last node
}//end back

template <class Type>
void linkedListType<Type>::copyList
(const linkedListType<Type>& otherList)
{
nodeType<Type> *newNode; //pointer to create a node
nodeType<Type> *current; //pointer to traverse the list
if (first != NULL) //if the list is nonempty, make it empty
destroyList();
if (otherList.first == NULL) //otherList is empty
{
first = NULL;
last = NULL;
count = 0;
}
else
{
current = otherList.first; //current points to the
//list to be copied
count = otherList.count;
//copy the first node
first = new nodeType<Type>; //create the node
first->info = current->info; //copy the info
first->link = NULL; //set the link field of the node to NULL
last = first; //make last point to the first node
current = current->link; //make current point to the next
// node
//copy the remaining list
while (current != NULL)
{
newNode = new nodeType<Type>; //create a node
newNode->info = current->info; //copy the info
newNode->link = NULL; //set the link of newNode to NULL
last->link = newNode; //attach newNode after last
last = newNode; //make last point to the actual last
//node
current = current->link; //make current point to the
//next node
}//end while
}//end else
}//end copyList

template <class Type>
linkedListType<Type>::~linkedListType() //destructor
{
destroyList();
}

template <class Type>
linkedListType<Type>::linkedListType
(const linkedListType<Type>& otherList)
{
first = NULL;
copyList(otherList);
}//end copy constructor

//overload the assignment operator
template <class Type>
const linkedListType<Type>& linkedListType<Type>::operator=
(const linkedListType<Type>& otherList)
{
if (this != &otherList) //avoid self-copy
{
copyList(otherList);
}//end else
return *this;
}
template <class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the new node
newNode->info = newItem; //store the new item in the node
newNode->link = first; //insert newNode before first
first = newNode; //make first point to the actual first node
count++; //increment count
if (last == NULL) //if the list was empty, newNode is also
//the last node in the list
last = newNode;
}//end insertFirst

template<class Type>
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if (first == NULL) //Case 1; the list is empty.
cout << "Cannot delete from an empty list."
<< endl;
else
{
if (first->info == deleteItem) //Case 2
{
current = first;
first = first->link;
count--;
if (first == NULL) //the list has only one node
last = NULL;
delete current;
}
else //search the list for the node with the given info
{
found = false;
trailCurrent = first; //set trailCurrent to point
//to the first node
current = first->link; //set current to point to
//the second node
while (current != NULL && !found)
{
if (current->info != deleteItem)
{
trailCurrent = current;
current = current-> link;
}
else
found = true;
}//end while
if (found) //Case 3; if found, delete the node
{
trailCurrent->link = current->link;
count--;
if (last == current) //node to be deleted was the
//last node
last = trailCurrent; //update the value of last
delete current; //delete the node from the list
}
else
cout << "The item to be deleted is not in "
<< "the list." << endl;
}//end else
}//end else
}//end deleteNode
1) Please format your code to make it readable. You will help yourself, and us, to follow your code better if you adopt a sensible indentation style. That will help you see the flow of control more easily, and to spot errors more easily.

2) What do you mean by "I cannot delete any node"? What behaviour are you seeing, and how does that behaviour differe from what you expect to see?

Give us a clear, complete, precise description of the problem, so that we can help you.
Could you please test this version?

linkedListType.hpp:
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
#ifndef BOPAKI_LINKEDLISTTYPE_HPP
#define BOPAKI_LINKEDLISTTYPE_HPP


template <class Type>
struct nodeType
{
    Type info;
    nodeType<Type> * link { nullptr };
};


template <class Type>
class linkedListType {
public:
    linkedListType();
    linkedListType(const linkedListType<Type>& otherList);
    const linkedListType<Type>& operator= (const linkedListType<Type> &);
    ~linkedListType();

    void initializeList();
    bool isEmptyList() const;
    void print() const;
    int length() const;
    void destroyList();
    Type front() const;
    Type back() const;
    // bool search(const Type& searchItem) const;
    void insertFirst(const Type& newItem);
    void insertLast(const Type& newItem);
    void deleteNode(const Type& deleteItem);

protected:
    int count;              // how many elements
    nodeType<Type> * first; // first node of the list
    nodeType<Type> * last;  // last node of the list

private:
    void copyList(const linkedListType<Type>& otherList);
};


#endif // BOPAKI_LINKEDLISTTYPE_HPP 



linkedListType.cpp:
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
#include "linkedListType.hpp"
#include <cassert>
#include <iostream>


// No argument Constructor:
template <class Type>
linkedListType<Type>::linkedListType()
    : count {}
    , first { nullptr }
    , last { nullptr }
{
}


// Copy Constructor
template <class Type>
linkedListType<Type>::linkedListType (const linkedListType<Type>& otherList)
{
    first = nullptr;
    copyList(otherList);
}


// overload the assignment operator
template <class Type>
const linkedListType<Type>&
linkedListType<Type>::operator=(const linkedListType<Type>& otherList)
{
    if (this != &otherList) {   //avoid self-copy
        copyList(otherList);
    }
    return *this;
}


// Destructor:
template <class Type>
linkedListType<Type>::~linkedListType()
{
    destroyList();
}


template <class Type>
void linkedListType<Type>::initializeList()
{
    destroyList(); //if the list has any nodes, delete them
}


template <class Type>
bool linkedListType<Type>::isEmptyList() const
{
    // Shouldn't this be done by means of 'count'?
    return first == nullptr;
}


template <class Type>
void linkedListType<Type>::print() const
{
    
    for ( nodeType<Type> * current { first };
          current != nullptr;
          current = current->link )
    {
        std::cout << current->info << ' ';
    }
}


template <class Type>
int linkedListType<Type>::length() const
{
    return count;
}


template <class Type>
void linkedListType<Type>::destroyList()
{
    nodeType<Type> * temp;
    while (first != nullptr)
    {
        temp = first;
        first = first->link;
        delete temp;
    }
    last = nullptr;
    count = 0;
}


template <class Type>
Type linkedListType<Type>::front() const
{
    assert(first != nullptr);
    return first->info;
}


template <class Type>
Type linkedListType<Type>::back() const
{
    assert(last != nullptr);
    return last->info;
}


template <class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
    nodeType<Type> * newNode { new nodeType<Type> };
    newNode->info = newItem; // store the new item in the node
    newNode->link = first; // insert newNode before first
    first = newNode; // make first point to the actual first node
    ++count;
    if (last == nullptr) {
        last = newNode;
    }
}


template<class Type>
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
    //Case 1: the list is empty
    if (first == nullptr) {
        std::cout << "Cannot delete from an empty list.\n";
        return;
    }

     // Case 2: the requested item is the first of the list:
    if (first->info == deleteItem)
    {
        nodeType<Type> * current { first };
        first = first->link;
        --count;
        if (first == nullptr) { // the list had only one node
            last = nullptr;
        }
        delete current;
        return;
    }

    // Case 3: search the list for the node with the given info:
    nodeType<Type> * trailCurrent { first };
    nodeType<Type> * current { first->link };
    bool found { false };

    while (current != nullptr && !found)
    {
        if (current->info != deleteItem)
        {
            trailCurrent = current;
            current = current-> link;
        }
        else {
            found = true;
        }
    }

    if (found)
    {
        trailCurrent->link = current->link;
        --count;
        if (last == current) {
            last = trailCurrent;
        }
        delete current;
        return;
    }

    std::cout << "The item to be deleted is not in the list.\n";
}


template <class Type>
void linkedListType<Type>::copyList (const linkedListType<Type>& otherList)
{
    if (first != nullptr) {
        destroyList();
    }
    if (otherList.first == nullptr)
    {
        first = nullptr;
        last = nullptr;
        count = 0;
        return;
    }

    nodeType<Type> * current { otherList.first };
    count = otherList.count;

    //copy the first node
    first = new nodeType<Type>;
    first->info = current->info;
    first->link = nullptr;
    last = first;
    current = current->link;

    //copy the remaining list
    nodeType<Type> *newNode;
    while (current != nullptr)
    {
        newNode = new nodeType<Type>;
        newNode->info = current->info;
        newNode->link = nullptr;
        last->link = newNode;
        last = newNode;
        current = current->link;
    }
}


template class linkedListType<int>;



main.cpp:
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
#include "linkedListType.hpp"
#include <iostream>


int main()
{
    std::cout << "Line 7: Enter numbers (ending with -999): ";
    linkedListType<int> list1;
    for (int num; std::cin >> num && num != -999; /**/)
    {
        list1.insertFirst(num);
    }

    std::cout << "\nLine 15: list1: ";
    list1.print();

    linkedListType<int> list2;
    list2 = list1;
    std::cout << "\nLine 19: list2: ";
    list2.print();

    std::cout << "\nLine 22: Enter the number to be deleted: ";
    int num;
    std::cin >> num;
    list2.deleteNode(num);

    std::cout << "\nLine 26: After deleting " << num << ", list2:\n";
    list2.print();
    std::cout << '\n';
    return 0;
}


Output:
Line 7: Enter numbers (ending with -999): 11 22 33 44 55 66 -999

Line 15: list1: 66 55 44 33 22 11
Line 19: list2: 66 55 44 33 22 11
Line 22: Enter the number to be deleted: 44

Line 26: After deleting 44, list2:
66 55 33 22 11

Your code works for me.
Thank you Enoizat your version works fine.

I just do not know why my code works fine for dhayden
but does not work for me.

I wish it could be pointed out to me as to why mine does not work!!!
I have replaced my deleteNode function with that of Enoizat's
but I still cannot get a node deleted when running the code.
Now I really do not know where the problem is with my code.
¿what's your test input?
¿what behaviour are you seeing?
¿does your code crash? provide a backtrace
¿is the output wrong? provide the output

you were lucky enough to find a testcase that breaks your code, don't withhold that information
Bopaki, as dhayden said, your code works.
You need to uncomment the line
 
//list2.deleteNode(num); //Line 25 

in main(), of course.

Example output:
Line 7: Enter numbers ending with -999.
11 22 33 44 55 66 -999

Line 15: list1: 66 55 44 33 22 11
Line 19: list2: 66 55 44 33 22 11
Line 22: Enter the number to be deleted: 44

Line 26: After deleting 44, list2:
66 55 33 22 11

Thank you all!!! Problem sorted out
Registered users can post here. Sign in or register to post.