suncica2222 0 Junior Poster

Can someone coment this code in detail, I can't quite understand algorithm behind this?

typedef struct node{
int x;
int y;
int value;
struct node* row;
struct node* col;
}node;

typedef struct matrix{
int height;
int width; 
node** rowList;
node** colList;
}matrix;

void insert(matrix** M, int row_index, int col_index, int value)
{
    node* currNode=(node*)malloc(sizeof(node));
    currNode->x=row_index;
    currNode->y=col_index;
    currNode->value=value;

    if ((*M)->rowList[row_index] == NULL) { 
        currNode->row = NULL;
        (*M)->rowList[row_index] = currNode;
    }
    else if ((*M)->rowList[row_index]->y > col_index) {
        currNode->col = (*M)->rowList[row_index];
        (*M)->rowList[row_index] = currNode;
    }
    else if ((*M)->rowList[row_index]->y < col_index) { 
        node* rowptr = (node*)malloc(sizeof(node));
        rowptr = (*M)->rowList[row_index];
        while(rowptr->col!=NULL&&rowptr->col->y < col_index)
            rowptr=rowptr->col;     

        currNode->col=rowptr->col;
        rowptr->col=currNode;

    }

    if ((*M)->colList[col_index] == NULL) { 
        currNode->col = NULL;
        (*M)->colList[col_index] = currNode;
    }
    else
    if ((*M)->colList[col_index]->x > row_index) { 
        currNode->row = (*M)->colList[col_index];
        (*M)->colList[col_index] = currNode;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.