Why is my templeted linkedlist outputting '0' ?

I have been trying to figure this out for a few hours. any help will be appreciated.

NodeList Header file
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
 #pragma once
#include "Node.h"

template <class T>
class NodeList : public Node<T>
{
public:
	NodeList<T>();
	void addToHead(const T&);
	void printList();

private:
	Node<T>* head;
	int size;
};

template <class T>
NodeList<T>::NodeList() :Node<T>()
{
	this->next = nullptr;
}

template <class T>
void NodeList<T>::addToHead(const T& data)
{
	Node<T>* newOne = new Node<T>();

	if (head == NULL)
	{
		head = newOne;
	}
	else
	{
		newOne->next = head;
		head = newOne;
	}

	size++;
}

template <class T>
void NodeList<T>::printList()
{
	Node<T>* tp = head;

	while(tp != NULL)
	{
		std::cout << *tp << std::endl;
		tp = tp->next;
	}
}


Node header file
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
#pragma once
#include <iostream>
#include <string>
template <class T>
class Node
{
private:
	
	T data;

public:
	Node<T>(T data = NULL);
	Node<T>* next;
	friend std::ostream& operator<<(std::ostream& os, const Node<T>& c)
	{
		return os << "Data: " << c.data;
	};
};

template <class T>
Node<T>::Node(T n):data(n), next(nullptr)
{

}


Driver cpp file
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
#include "NodeList.h"
//#include "Node.h"


int main()
{
	NodeList<int>* nl1 = new NodeList<int>;

	int num;

	while (true)
	{
		std::cout << "Enter the name of the contact or -1 to quit.\n";
		std::cin >> num;
		if (num == -1)
		{
			break;
		}
		else
		{
			nl1->addToHead(num);
		}
	}

	nl1->printList();
	
	system("pause");
	return 0;
}
Last edited on
In your function NodeList<T>::addToHead(const T& data), where do you use the variable data?
here
 
nl1->addToHead(num);
I meant inside the function itself. You actually didn't use it at all.
1
2
Node<T>* newOne = new Node<T>();
Node<T>* newOne = new Node<T>(data);


Your head should be initialized to nullptr in the constructor, so make sure to add that. NULL isn't used for pointers anymore in modern C++.

1
2
3
4
5
6
7
8
  if (head == NULL)
  {
    head = newOne;
  }
  if (head == nullptr)
  {
    head = newOne;
  }


Output is now:


$ g++ main.cpp
$ ./a.out 
Enter the name of the contact or -1 to quit.
256
Enter the name of the contact or -1 to quit.
128
Enter the name of the contact or -1 to quit.
64
Enter the name of the contact or -1 to quit.
32
Enter the name of the contact or -1 to quit.
16
Enter the name of the contact or -1 to quit.
8
Enter the name of the contact or -1 to quit.
4
Enter the name of the contact or -1 to quit.
2
Enter the name of the contact or -1 to quit.
1
Enter the name of the contact or -1 to quit.
0
Enter the name of the contact or -1 to quit.
-1
Data: 0
Data: 1
Data: 2
Data: 4
Data: 8
Data: 16
Data: 32
Data: 64
Data: 128
Data: 256
$ 
Last edited on
Oh. Well that makes sense. Adding data to the node fixed it.

So my constructor should have this structure?

NodeList header
1
2
3
4
5
6
template <class T>
NodeList<T>::NodeList() :Node<T>()
{
	this->head = nullptr;
	this->next = nullptr;
}


Node Header
1
2
3
4
5
template <class T>
Node<T>::Node(T n):data(n), next(nullptr)
{

}
yes
Topic archived. No new replies allowed.