#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;
}
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
$