Linked list cannot access last position

1
2
3
4
5
6
7
8
9
10
11
12
template <typename T>
class node
{
	public:
		T nodeValue;
		node<T> *next;
		node() : next(NULL)
		{}
		node(const T& item, node<T> *nextNode = NULL) :
			nodeValue(item), next(nextNode)
		{}
};

this is my header file.

and

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
#include <iostream>
#include <list>
#include "d_node.h"

using namespace std;

template <typename T>
void writeLinkedList(node<T> *front, const string& separator = " ")
{
	node<T> *curr;
	curr = front;

	while (curr != NULL)
	{
		if(curr->nodeValue != 0 && curr->nodeValue != 1)
		{
			cout << curr->nodeValue << " ";
		}
		curr = curr->next;
	}
}

template <typename T>
void makeZero(node<T> *front, const T& item)
{
	node<T> *curr;
	curr = front;

	while (curr != NULL)
	{
		if (curr->nodeValue == item)
		{
			curr->nodeValue = 0;
		}
		curr = curr->next;
	}
}

int main() {

	int i;
	cout << "Enter an integer: ";
	cin >> i;

	node<int> *front = NULL, *newNode;

	newNode = new node<int> (1, front);
	front = newNode;

	for (int k = i; 0 < k; k--)
	{
		newNode = new node<int> (k, front);
		front = newNode;
	}

	for (int a = 2; a < 1000; a++)
	{
		for (int b = 2; a*b < i; b++)
		{
			int count = a*b;
			makeZero(front, count);
		}
	}

	writeLinkedList(front, " ");
	cout << endl;
}


and this is my main file. It's supposed to access every position and change the value to 0 unless it's not a prime number. However, for some reasons, it does not access the last position.

Topic archived. No new replies allowed.