Nunya Business Nunya Business - 15 days ago 5
C++ Question

Print Array of Linked Lists C++

I'm new to coding and I'm trying to implement an array of linked lists. I have the structure and array of lists populated, but my print function only ever prints the array index [0], first list element without ever moving the pointer to the second list element in array[0]. In essence it's an infinite loop that only ever prints that first element.

My question is this: How do you move the pointer to the next element in the list so I can finish printing the list and move the array index to the next index?

My structure looks like this:

struct Node
{
int Vertex;
Node* next;
};


and after inserting all the nodes and lists, my print function looks like this:

void printList(Node* adjList[5])
{
int a;
for (int b = 0; b <= 5; b++)
{
a = 0;
while (adjList[a]->next != NULL)
{
cout << "(" << adjList[a]->Vertex;
cout << ", " << adjList[a]->next->Vertex << ") ";
cout << a << endl;
system("pause");
}
a++;
}
cout << endl << endl;
}


Ignore the part where I print out "a" and then pause because I was trying to figure out a different problem I was having. But now all I think I need is how to move the pointer to the next element in the list at each array index.

EDIT: After reading the comment below, here is my main with the generation of a small portion of the list array:

int main()
{
Node *adjList[5];
adjList[0] = new Node;
adjList[0]->Vertex = 1;
adjList[0]->next = new Node;
adjList[0]->next->Vertex = 4;
adjList[1] = new Node;

...

printList(adjList);

Answer

It's hard to know what output you are expecting for your print function. However, the following code should output something correct with no crash. Note that I introduced recursion through printNode to make sure child nodes are printed!

void printNode(Node* node)
{
    if (node != NULL)
    {
        cout << "(" << node->Vertex;
        cout << ", ";
        printNode( node->next );
        cout << ")";
    }
    else
    {
        cout << "null"; 
    }
}

void printList(Node* adjList[5])
{
    for (size_t index = 0; index < 5; index++)
    {
        printNode(adjList[index]);
        cout << endl; 
    }
}

You must make sure that unused next attributes are set to NULL so that recursion can be stopped. So your initialization must be:

Node *adjList[5];
adjList[0] = new Node;
adjList[0]->Vertex = 1;
adjList[0]->next = new Node;
adjList[0]->next->Vertex = 4;
adjList[0]->next->next = NULL; // added, was uninitialized
adjList[1] = new Node;
...

By instance, this program:

int main()
{
    Node *adjList[5];
    adjList[0] = new Node;
    adjList[0]->Vertex = 1;
    adjList[0]->next = new Node;
    adjList[0]->next->Vertex = 4;
    adjList[0]->next->next = NULL; // added, was uninitialized
    adjList[1] = new Node;
    adjList[1]->Vertex = 6;
    adjList[1]->next = new Node;
    adjList[1]->next->Vertex = 7;
    adjList[1]->next->next = NULL; // added, was uninitialized
    adjList[2] = NULL;
    adjList[3] = NULL;
    adjList[4] = NULL;
    printList( adjList );

    return 0;
}

outputs:

(1, (4, null))
(6, (7, null))
null
null
null
Comments