LinkedList

Write your question here.
I'm writing code for a singly linked list for my data structures class and I can't figure out what I am doing wrong. Any help is much appreciated!

My teacher wants me to write all of the code in my LinkedList.h file with a LinkedListNode class nested in the LinkedList class.

Please let me know if you have any questions on what my functions are supposed to do or what my logic was in what I'm doing. I think I'm using the template wrong, but I'm not sure. Thanks again 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
  #pragma once

template <typename type>
class LinkedList
{
private:

	LinkedListNode *head;
	int count;

public:
	template <typename type>
	class <type>LinkedListNode
	{
	private:
		type item;
		LinkedListNode *next;

	public:
		<type>LinkedListeNode(const type& newItem)
		{
			item = newItem;
			next = NULL;
		}
	};
	LinkedList()
	{
		head = NULL;
		count = 0;
	}

	~LinkedList();

	/*
	insertHead

	A node with the given value should be inserted at the beginning of the list.

	DO NOT ALLOW DUPLICATE VALUES IN THE LIST.  If there is a duplicate item
	then throw an exception.
	*/
	virtual void insertHead(type value)
	{
		LinkedListNode* existing = find(value);
		if (existing == NULL)
		{
			LinkedListNode *n = new LinkedListNode(value);
			n->next = head;
			head = n;
			count++;
			delete existing;
		}
		else
		{
			delete existing;
			throw exception("Duplicate Item!")
		}
	}

	/*
	insertTail

	A node with the given value should be inserted at the end of the list.

	DO NOT ALLOW DUPLICATE VALUES IN THE LIST.  If there is a duplicate item
	then throw an exception.
	*/
	virtual void insertTail(type value)
	{
		LinkedListNode* existing = find(value);
		if (existing == NULL)
		{
			LinkedListNode *tail = head;
			LinkedListNode *n = new LinkedListNode(value);
			while (tail != NULL)
			{
				tail = tail->next;
			}
			tail->next = n;
			count++;
			delete tail;
			delete existing;
		}
		else
		{
			delete existing;
			throw exception("LinkedListNode already exists");
		}
	}

	/*
	insertAfter

	Insert newValue immediately after the first occurrence of
	searchValue (i.e., as you move through the list from head
	to tail).

	If no match is found, do not add the item to the list.  Do
	not throw an exception.
	*/
	virtual void insertAfter(type searchValue, type newValue)
	{
		LinkedListNode* existing = find(searchValue);
		if (existing == NULL)
		{
			delete existing;
			throw exception("invalid LinkedListNode");
		}
		else
		{
			LinkedListNode *n = new LinkedListNode(newValue);
			n->next = existing->next;
			existing->next = n;
			count++;
			delete exsisting;
		}
	}

	/*
	remove

	The node with the given value should be removed from the list.
	If there are two nodes with the same value, only remove the
	first one.

	The list may or may not include a node with the given value.
	*/
	virtual void remove(type searchValue)
	{
		LinkedListNode* existing = find(searchValue);
		if (existing == NULL)
		{
			delete existing;
			Throw exception("LinkedListNode does not exist.");
		}
		else
		{
			LinkedListNode *temp = head;
			while (temp->next != existing)
			{
				temp = temp->next;
			}
			temp->next = existing->next;
			delete existing;
			delete temp;
			count--;
		}
	}

	/*
	clear

	Remove all nodes from the list.
	*/
	virtual void clear()
	{
		while (head != NULL)
		{
			LinkedListNode *temp = head;
			head = temp->next;
			delete temp;
		}
		count = 0;
	}

	/*
	at

	Returns the value of the node at the given index. The list begins at
	index 0.

	If the given index is out of range of the list, throw an out of range exception.
	*/
	virtual type at(int index)
	{
		if (index > size() || index < 0)
			throw exception("Out of Range.");
		else if (index == 0)
			return head->item;
		else
		{
			LinkedListNode *search = head;
			type tempItem;
			for (int i = 0; i < index; i++)
			{
				search = search->next;
			}
			tempItem = search->item;
			delete search;
			return tempItem;
		}
	}

	/*
	size

	Returns the number of nodes in the list.
	*/
	virtual int size()
	{
		LinkedListNode *temp = head;
		int length = 0;
		while (temp != NULL)
		{
			length++;
			temp = temp->next;
		}
		count = length;
		return length;
	}

	LinkedListNode* find(type node)
	{
		if (head == NULL)
			return NULL;
		LinkedListNode *temp = head;
		while (temp != NULL)
		{
			if (temp->item == node)
				return temp;
			temp = temp->next;
		}
		delete temp;
		return NULL;

	}
};
Topic archived. No new replies allowed.