Using linked lists, where is the error?

0

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;
}
    
asked by Gabriel Valedon 23.04.2017 в 19:23
source

1 answer

0

You can not name the variables using reserved words, new in this case:

void addNode(nNode &list, int info)
{
    nNode new;
    new = new(struct newNode);
    new->info = info;
    new->link = list;
    list = new;
}

Fixing this, the program works perfectly:

void addNode(nNode &list, int info)
{
    nNode nuevo;
    nuevo = new(struct newNode);
    nuevo->info = info;
    nuevo->link = list;
    list = nuevo;
}
    
answered by 24.04.2017 / 00:08
source