Beginner_B Beginner_B - 3 months ago 7
C++ Question

How to store a list of numbers in a link list

I have been trying to store the numbers of the array in a link list. But I don't know how to do it. I need someone to help me finish the code.

#include <iostream>


using namespace std;

int main()
{
int numeros[9] = {1,2,3,4,5,6,7,8,9};

typedef struct Node *NodePtr; //declara Nodeptr un apuntador a Node
struct Node
{
int x;
Node *next; // omit the 'struct' for C++-only usage
};

return 0;
}

Answer

The compact way:

struct Node
{
    int x;
    Node *next;
} nodes[] = {
    {1, nodes + 1},
    {2, nodes + 2},
    {3, nodes + 3},
    {4, nodes + 4},
    {5, nodes + 5},
    {6, nodes + 6},
    {7, nodes + 7},
    {8, nodes + 8},
    {9, nullptr}
};
Node* root = nodes;