简体   繁体   中英

Sparse Matrix print from linked list

I'm trying to print out the sparse matrix from my linked list. That looks like this:

0 0 0 0 0 0 
1 0 6 0 0 0 
4 0 0 0 6 0

But for this, it just prints out 0 with the value inside. Here's the codes.

    while (temp != NULL)
{

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            if ((row == (temp -> e).getRow()) && (col == (temp -> e).getCol()))
                cout << temp ->e.getValue();
            else
                cout << "0";
        }
            cout << endl;
    }


    temp = temp -> next;
}

i and j are being incremented. Those are the values you need to check against.

You're comparing with row and col which are the max values and will never be reached.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM