Greetings! Practicing chained lists, I can not find the error in the following code:
#include <iostream>
#include <string>
#include <list>
using namespace std;
struct newNode
{
int info;
struct newNode *link;
};
typedef struct newNode *nNode;
void addNode(nNode &list, int info)
{
nNode new;
new = new(struct newNode);
new->info = info;
new->link = list;
list = new;
}
void printList(nNode list)
{
int count = 0;
while(list != NULL)
{
cout << "[" << list->info << "]->";
list = list->link;
count++;
}
cout << "The length of the List is: " << count << endl;
}
int main()
{
nNode list = NULL;
addNode(list, 1);
addNode(list, 2);
addNode(list, 3);
addNode(list, 4);
addNode(list, 5);
printList(list);
return 0;
}