Single Linked List

Hey guys I am supposed to write a cpp file with the implementation of a single linked list data structure that I am to define in an h file. The implementation of this list data structure has these functions:

List() – create an empty linked list by setting the external pointer (head) to NULL thus
signifying an empty List.
~List() – deallocate every node in the linked list and set the external pointer (head) back
to NULL
bool Insert (string) – create a dynamic node and store the string passed within it; insert
the node into its correct alpha location in the list; return true if successful and false if
memory allocation failed
bool Delete (string) – delete the node containing the string passed; return true if the
deletion was successful and false if the string could not be found in the list
bool Edit (string, string) – the function accepts two strings, 1) the string to be edited and
2) the edited text; locate the string to be edited and then edit the text; note that when the
text is edited, it may now have to be placed in a different location in the list. Consider
executing the algorithm by performing first a delete and then an insert. Return true if the
edit was successful and false otherwise.
void Print () – print the list to the screen one data element (string) per line

main() is in another cpp file that my teacher provided to test these functions. It successfully compiles but then crashes when I try to use one of the functions. Could you guys tell me what I'm doing wrong?

list.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
#pragma once
#include <iostream>
#include "List.h"
using namespace std;

List::List()
{
	head = NULL;
}

List::~List()
{
	node * temp = head;
	while (head != NULL)
	{
		temp = head->next;
		delete head;
		head = temp;
	}
}

bool List::Delete(string oldItem)
{
	node * temp;
	node* trailor; 
	if (head->item == oldItem)
	{
		temp = head;
		head = head->next;
		delete head;
		delete temp;
		return true;
	}
	else
	{
		temp = head;
		while (head != NULL)
		{
			if (head->next->item == oldItem)
			{
				trailor = head;
				trailor->next = head->next->next;
				delete head->next;
				head = temp;
				delete temp;
				return true;
			}
			head = head->next;
		}
		return false;
	}
}

bool List::Insert(string newItem)
{
	node * newNode = new node;
	newNode->item = newItem;
	if (newNode == NULL)
	{
		return false;
	}

	if (head == NULL)
	{
		head = newNode;
		newNode->next = NULL;
		return true;
	}
	else if (newNode->item < head->item)
	{
			newNode->next = head;
			head = newNode;
			return true;
	}
	else
	{	
		node * temp = new node;
		for (temp = head; temp != NULL; temp = temp->next)
		{
			if (newNode->item > temp->item);
			{
				newNode->next = temp->next;
				temp->next = newNode;
				delete temp;
				
				return true;
			}
		}
	}
}

bool List::Edit(string item, string itemNew)
{
	if (Delete(item))
	{
		if (Insert(itemNew))
		{
			return true;
		}
	}
	return false;
}

void List::Print()
{
	node * temp;
	for (temp = head; head != NULL; head = head->next)
	{
		cout << head->item << endl;
	}
	head = temp;
	delete temp;
}



list.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

struct node
{
		string item;
		node * next;
};

class List
{
private:
	node * head;

public:

	List();
	~List();
	bool Insert(string);
	bool Delete(string);
	bool Edit(string, string);
	void Print();
};

Last edited on
First, please edit your first post to include code tags. Highlight the code in each of the files and click the Format button that looks like "<>". This will make it easier to read and comment on your code.

Second,

main() is in another cpp file that my teacher provided to test these functions. It successfully compiles but then crashes when I try to use one of the functions. Could you guys tell me what I'm doing wrong?


Could you be a little bit more specific? Which "one of the functions" causes it to crash.

In InsertI do see a problem--In the last "else" clause you are allocating a new node then reassigning the pointer (this is a memory leak) and then deallocating the pointer, which is pointing to a node in the list. This is certainly a problem.

You want to declare a pointer for traversing through the list (like you do in Print) without allocating memory for it here.
Topic archived. No new replies allowed.