ADT linked list not working

I am using a linked list to make a to do list. I am having trouble with by Delete function in my implementation. The delete function is being passed a string data of the node i want to delete from the list. This function should find the item in the list and then delete the node but not creating a memory leak in the process. So if i delete an item from the middle of the list, I can still see the whole list when I print out the list.

bool List::Delete(string data)
{
Node *temp = new Node;
temp->data = data;
trailer = NULL;
cur = head;
while (cur != temp)
{
trailer = cur;
cur = cur->next;
}
trailer->next = cur->next;
if (cur->next == NULL)
{
trailer->next == NULL;

}
delete temp;
if (temp)
{
return false;
}
else
{
return true;
}

}
It's impossible to tell whether that code is incorrect or where to change it if we don't know what your data members you have and what they mean.
Here is my header:

#include <string>
using namespace std;

struct Node
{
string data;
Node * next;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class List
{
public:
	List();
	~List();
	bool Insert(string);
	bool Delete(string);
	void Print();
	bool Edit(string, string);
private:
	Node * head;
	Node * cur;
	Node * trailer;
};


Here is the full 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
List::List():head(NULL)
{}
List::~List()
{
	Node *temp = head->next;
	while (head != NULL)
	{
		delete head;
		head = temp;
		temp = head->next;
	}
}
bool List::Insert(string data)
{
	Node* newNode = new Node;

	if (newNode == NULL)
	{
		cout << "Error: Memory Allocation Failed" << endl;
		return false;
	}

	newNode->data = data;
	if (head == NULL)
	{
		//cout << "head is Null" << endl;
		head = newNode;
		cout << head -> data << endl;
		newNode->next = NULL;
		return true;
	}
	cur = head;
	trailer = NULL;

	while (newNode->data > cur->data && cur -> next != NULL)
	{
		trailer = cur;
		cur = cur->next;

	}
	if (trailer == NULL)
	{
		newNode->next = head;
		head = newNode;
		return true;
	}
	if (cur->next == NULL)
	{
		cur->next = newNode;
		newNode->next = NULL;
		return true;
	}

	else
	{
		trailer->next = newNode;
		newNode->next = cur;
	}
		return true;

}


bool List::Delete(string data)
{
	Node *temp = new Node;
	temp->data = data;
	trailer = NULL;
	cur = head;
	while (cur != temp)
	{
		trailer = cur;
		cur = cur->next;
	}
	trailer->next = cur->next;
	if (cur->next == NULL)
	{
		trailer->next == NULL;

	}
	delete temp;
	if (temp)
	{
		return false;
	}
	else
	{
		return true;
	}

}
bool List::Edit(string dataDelete, string dataInsert)
{
	Delete(dataDelete);
	Insert(dataInsert);
	return true;
}
void List::Print()
{
	for (Node * Count = head; Count != NULL; Count = Count->next)
	{
		cout << Count->data << endl;
	}
}


The text file that is used is:
1
2
3
4
5
6
pay bills
make dental appt
daily bible reading
get estimate on car repair
mail thank-you notes
take test



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
bool List::Delete(string data)
{
	Node *temp = new Node; //¿? You want to remove a node, ¿why are you creating another then?
	temp->data = data;
	trailer = NULL; //limit the scope of your variables. I don't see any good reason for them to be members of the class
	cur = head;
	while (cur != temp) //tautology
	{
		trailer = cur;
		cur = cur->next;
	}
	trailer->next = cur->next;
	if (cur->next == NULL)
	{
		trailer->next == NULL; //read the warnings (enable the warnings)

	}
	delete temp;
	if (temp) //`delete' would not set the pointer to null, tautology
	{
		return false;
	}
	else
	{
		return true;
	}

}



> The text file that is used
you kind of forgot the code that uses that file
Last edited on
If your Node had a constructor, it would remove all that initialization code.

new will never return null, so all that code that handles it will never be called. A modern compiler really should warn about it, but they don't.

List constructor doesn't initialize all its members. As ne555 said, you don't need all those members, just head as you have a single threaded linked list.

As looking up a node is possibly a common thing, you'd simplify your code if you had a private method Node* Find(const std::string& s) const, that did the lookup.

Delete would then be:
1
2
3
4
5
6
7
8
9
10
bool List::Delete(const std::string& data)
{
    if (Node* p = Find(data))
    {
        /* delete the node */
        return true;
    }

    return false;
}


Pass strings by const ref rather than by value. You're creating unnecessary copies.
Topic archived. No new replies allowed.