Linked List Functions Trouble

I am experiencing trouble trying to create a unit test for all the functions in my Linked List. Mainly I'm having trouble with my insert function that builds but just does not insert, and my First function which just returns the data in the first slot of my Linked List.

I am getting the following error for my code: newNodeCatch = myList.First();

Error C2440 'return': cannot convert from 'int' to 'ListNode<T> *' UnitTest

PS. My teacher helped to create the Linked List with me so I assume it is all correct code but I find it quite difficult to follow.

PPS. Thank you for any replies at all.

The Code:

//==========================================================================
// source.cpp
//==========================================================================

void TestLinkedList()
{
printf("\n====================================================\n");
printf(" LINKED LIST FUNCTIONS TEST");
printf("\n====================================================\n\n");

LinkedList<int> myList;

Iter<int> iter;

printf("\nCreating and populating a new LinkedList . . .\n");

myList.PushFront(6);
myList.PushFront(3);
myList.PushFront(1);

myList.PushBack(78);
myList.PushBack(12);
myList.PushBack(34);

printf("Testing operator overloads, peek at the code to see!\n");

cout << endl << "Current values are" << endl;

iter = myList.Begin(); //=

while (iter != myList.End()) // != ==
{
std::cout << iter.value() << std::endl; //value()
++iter; // --
}

ListNode<int>* newNode = new ListNode<int>;

newNode->data = 5;

myList.Insert(newNode);

//moving the iter back to the start
while (iter != myList.Begin()) // != ==
{
--iter; // --
}

cout << endl << "Current values are" << endl;
while (iter != myList.End()) // != ==
{
std::cout << iter.value() << std::endl; //value()
++iter; //++ --
}

ListNode<int>* newNodeCatch = new ListNode<int>;

newNodeCatch = myList.First();
}

int main()
{
TestLinkedList();

system("pause");
return 0;
}

//==========================================================================
// LinkedList.h
//==========================================================================

#pragma once
#include "ListNode.h"
#include <crtdbg.h>

template <typename T>
class Iter
{
ListNode<T>* current;

public:

//=
Iter& operator=(ListNode<T>* other)
{
//LinkedList<int> other1;
current = other;
return *this;
}

//!=
bool operator!=(ListNode<T>* other)
{
if (current != other)
return true;
else
return false;
}

//==
bool operator==(ListNode<T>* other)
{
if (current == other)
return true;
else
return false;
}

//->
T value()
{
return current->data;
}

//++
void operator++()
{
current = current->next;
}

//--
void operator--()
{
current = current->prev;
}
};

template <typename T>
class LinkedList
{
public:

LinkedList()
{
start = new ListNode<T>();
_ASSERT(start);
end = new ListNode<T>();
_ASSERT(end);

start->next = end;
end->prev = start;

start->prev = nullptr;
end->next = nullptr;
}

~LinkedList()
{
delete end;
delete start;
}

void PushFront(T data)
{
ListNode<T>* node = new ListNode<T>();
_ASSERT(node);
node->data = data;
node->next = start->next;
start->next->prev = node;
start->next = node;
node->prev = start;
}

void PushBack(T data)
{
ListNode<T>* node = new ListNode<T>();
_ASSERT(node);
node->data = data;
node->prev = end->prev;
end->prev->next = node;
end->prev = node;
node->next = end;
}

void Insert(ListNode<T>* node)
{
ListNode<T>* firstNode = node;
ListNode<T>* lastNode = node->next;
ListNode<T>* newNode = new ListNode<T>*;
//ListNode<T>* newNode = new ListNode<T>;
_ASSERT(newNode);

firstNode = newNode->prev;
lastNode = newNode->next;

//delete node;
}

ListNode<T>* Begin()
{
return start->next;
}

ListNode<T>* End()
{
return end;
}

ListNode<T>* First()
{
_ASSERT(start->next != end);
return start->next->data;
}

ListNode<T>* Last()
{
_ASSERT(end->prev != start);
return end->prev->data;
}

int Count()
{
int m_count = 0;

ListNode<T>* current = start->next;

while (current != end)
{
++current;
++m_count;
}

return m_count;
}

ListNode<T>* Delete(ListNode<T>* node)
{
ListNode<T>* prevNode = node->prev;
ListNode<T>* nextNode = node->next;

prevNode->next = nextNode;
nextNode->prev = prevNode;

delete node;

return nextNode;
}

void Erase(Iter<T> & target)
{
target.current = Delete(target.current);
}

void Remove(T value)
{
ListNode<T>* current = start->next;

while (current != end)
{
if (current.data == value)
{
current = Delete(current);
}

else
{
++current;
}
}
}

void PopBack()
{
_ASSERT(end->prev != start);
Delete(end->prev);
}

void PopFront()
{
_ASSERT(start->next != end);
Delete(start->next);
}

void Clear()
{
ListNode<T>* current = start->next;

while (current != end)
{
current = Delete(current);
++current;
}
}

private:
ListNode<T>* start;
ListNode<T>* end;
int m_Iterator;
};

//==========================================================================
// ListNode.h
//==========================================================================

#pragma once
#include "ListNode.h"

template <typename T>
class ListNode
{
public:
ListNode() {};
~ListNode() {};

T data;
ListNode* next;
ListNode* prev;
};
I am getting the following error for my code: newNodeCatch = myList.First();

Error C2440 'return': cannot convert from 'int' to 'ListNode<T> *' UnitTest

Should First() and Last() return a value or a pointer to a node? If you want them to return a value you need to change the return type of those two functions from ListNode<T>* to T, and change the code in TestLinkedList() so that you assign the return value of myList.First() to an int variable.
My teacher helped to create the Linked List with me so I assume it is all correct code

Well there's your problem :).

Insert() doesn't seem to do anything.

After putting it all in one file (for my own convenience) and fixing the compiler errors, it seems to work. Note that I had to #define _ASSERT because my compiler doesn't support it.

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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// #include "ListNode.h"

#define _ASSERT(a)

template < typename T > class ListNode {
  public:
    ListNode() {
    };
    ~ListNode() {
    };

    T data;
    ListNode *next;
    ListNode *prev;
};

//==========================================================================
// LinkedList.h
//==========================================================================

// #include "ListNode.h"
// #include <crtdbg.h>

template < typename T > class Iter {
    ListNode < T > *current;

  public:

//=
    Iter & operator=(ListNode < T > *other) {
//LinkedList<int> other1;
	current = other;
	return *this;
    }

//!=
    bool operator!=(ListNode < T > *other)
    {
	if (current != other)
	    return true;
	else
	    return false;
    }

//==
    bool operator==(ListNode < T > *other)
    {
	if (current == other)
	    return true;
	else
	    return false;
    }

//->
    T value()
    {
	return current->data;
    }

//++
    void operator++()
    {
	current = current->next;
    }

//--
    void operator--()
    {
	current = current->prev;
    }
};


template < typename T > class LinkedList {
public:

    LinkedList() {
	start = new ListNode < T > ();
	_ASSERT(start);
	end = new ListNode < T > ();
	_ASSERT(end);

	start->next = end;
	end->prev = start;

	start->prev = nullptr;
	end->next = nullptr;
    }

    ~LinkedList() {
	delete end;
	delete start;
    }

    void PushFront(T data)
    {
	ListNode < T > *node = new ListNode < T > ();
	_ASSERT(node);
	node->data = data;
	node->next = start->next;
	start->next->prev = node;
	start->next = node;
	node->prev = start;
    }

    void PushBack(T data)
    {
	ListNode < T > *node = new ListNode < T > ();
	_ASSERT(node);
	node->data = data;
	node->prev = end->prev;
	end->prev->next = node;
	end->prev = node;
	node->next = end;
    }

    void Insert(ListNode < T > *node)
    {
	ListNode < T > *firstNode = node;
	ListNode < T > *lastNode = node->next;
	// ListNode < T > *newNode = new ListNode < T >;
	ListNode<T>* newNode = new ListNode<T>;
	_ASSERT(newNode);

	firstNode = newNode->prev;
	lastNode = newNode->next;

//delete node;
    }

    ListNode < T > *Begin() {
	return start->next;
    }

    ListNode < T > *End() {
	return end;
    }

    ListNode < T > *First() {
	_ASSERT(start->next != end);
	// return start->next->data;   // dmh
	return start->next;
    }

    ListNode < T > *Last() {
	_ASSERT(end->prev != start);
	// return end->prev->data;  // dmh
	return end->prev;
    }

    int Count()
    {
	int m_count = 0;

	ListNode < T > *current = start->next;

	while (current != end) {
	    ++current;
	    ++m_count;
	}

	return m_count;
    }

    ListNode < T > *Delete(ListNode < T > *node) {
	ListNode < T > *prevNode = node->prev;
	ListNode < T > *nextNode = node->next;

	prevNode->next = nextNode;
	nextNode->prev = prevNode;

	delete node;

	return nextNode;
    }

    void Erase(Iter < T > &target)
    {
	target.current = Delete(target.current);
    }

    void Remove(T value)
    {
	ListNode < T > *current = start->next;

	while (current != end) {
	    if (current.data == value) {
		current = Delete(current);
	    }

	    else {
		++current;
	    }
	}
    }

    void PopBack()
    {
	_ASSERT(end->prev != start);
	Delete(end->prev);
    }

    void PopFront()
    {
	_ASSERT(start->next != end);
	Delete(start->next);
    }

    void Clear()
    {
	ListNode < T > *current = start->next;

	while (current != end) {
	    current = Delete(current);
	    ++current;
	}
    }

  private:
    ListNode < T > *start;
    ListNode < T > *end;
    int m_Iterator;
};

//==========================================================================
// source.cpp
//==========================================================================

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void
TestLinkedList()
{
    cout <<"\n====================================================\n";
    cout <<" LINKED LIST FUNCTIONS TEST";
    cout <<"\n====================================================\n\n";

    LinkedList < int >myList;

    Iter < int >iter;

    cout <<"\nCreating and populating a new LinkedList . . .\n";

    myList.PushFront(6);
    myList.PushFront(3);
    myList.PushFront(1);

    myList.PushBack(78);
    myList.PushBack(12);
    myList.PushBack(34);

    cout <<"Testing operator overloads, peek at the code to see!\n";

    cout << endl << "Current values are" << endl;

    iter = myList.Begin();			 //=

    while (iter != myList.End())		 // != ==
    {
	std::cout << iter.value() << std::endl;	 //value()
	++iter;					 // --
    }

    ListNode < int >*newNode = new ListNode < int >;

    newNode->data = 5;

    myList.Insert(newNode);

//moving the iter back to the start
    while (iter != myList.Begin())		 // != ==
    {
	--iter;					 // --
    }

    cout << endl << "Current values are" << endl;
    while (iter != myList.End())		 // != ==
    {
	std::cout << iter.value() << std::endl;	 //value()
	++iter;					 //++ --
    }

    ListNode < int >*newNodeCatch = new ListNode < int >;

    newNodeCatch = myList.First();
}

int
main()
{
    TestLinkedList();

    system("pause");
    return 0;
}

Sorry to say dhayden I just ran your edited code and It does not seem to work. The output is the same before the insert.

http://puu.sh/wsM3s/a361141e36.png
I modified the test function and it failed already at the 2. assertion.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void TestLinkedList()
{
    cout <<"\n====================================================\n";
    cout <<" LINKED LIST FUNCTIONS TEST";
    cout <<"\n====================================================\n\n";

    LinkedList <int> myList;
    _ASSERT(myList.Count() == 0);

    myList.PushBack(1);
    _ASSERT(myList.Count() == 1);

    auto first = myList.First();
    _ASSERT(first != nullptr);
    _ASSERT(first->data == 1);
}


@dhayden
I think a better way to deal with the missing _ASSERT is:
1
2
3
#ifndef _ASSERT
#define _ASSERT(cnd) assert(cnd)
#endif 

In this way all the assertions will work.
Here is fixed version of Insert():
1
2
3
4
5
6
7
    void Insert(ListNode < T > *node)
    {
        node->next = start->next;
        start->next->prev = node;
        start->next = node;
        node->prev = start;
    }

I stole the code directly from PushFront, which now becomes:
1
2
3
4
5
6
7
    void PushFront(T data)
    {
        ListNode < T > *node = new ListNode < T > ();
        _ASSERT(node);
        node->data = data;
        Insert(node);
    }
Yeah, I suppose that still matches the requirements. Guess the teachers can't mark me down for that but I do have the feeling I'm supposed to use the Iter class...
Still doesn't work.
_ASSERT(myList.Count() == 1); fails
Remove the asserts you don't need them. It's just a visual studio thing. They do not change the code at all.
Of course they don't change the code, they only show that's sth. wrong. They are universal, _ASSERT is VS but assert is standard C++.
Look at his code
First I create an empty list and list.Count() == 0 which is correct.
Then I add one value and the count is still 0, if this is not wrong what is it then ????
1
2
3
4
5
LinkedList <int> myList;
  _ASSERT(myList.Count() == 0);

  myList.PushBack(1);
  _ASSERT(myList.Count() == 1);
Topic archived. No new replies allowed.