-1

我正在尝试创建一个二维链表来保存一个稀疏矩阵,并编写了这段代码来在正确的位置插入一个新节点:

void insertNewNode(node **rowHead, node **columnHead, int value, int row, int column) { 
    //Get to the correct position in the column linked list
    if (*columnHead == NULL) {
        *columnHead = malloc(sizeof(node));
    } else {
        while((*columnHead)->nextColumn != NULL && (*columnHead)->nextColumn->row < row)
            *columnHead = (*columnHead)->nextColumn;
    }

    //Get to the correct position in the row linked list.
    if (*rowHead == NULL) {
        *rowHead = malloc(sizeof(node));
    } else {
        while((*rowHead)->nextRow != NULL && ((*rowHead)->nextRow->column < column))
            *rowHead = (*rowHead)->nextRow;
    }

    node *newNode = malloc(sizeof(node));
    newNode->column = column;
    newNode->row = row;
    newNode->value = value;

    (*columnHead)->nextColumn = newNode;
    (*rowHead)->nextRow = newNode;
}

出于某种原因,最后一行:

(*rowHead)->nextRow = newNode;

导致 EXC_BAD_ACCESS 错误,而前一行不是,我不完全确定为什么。谁能看到会发生这种情况的原因?

4

1 回答 1

1

这可能只是程序中其他地方的问题,即如何分配/维护行数据,而列数据恰好没问题。你检查过 rowHead 的值吗?也许它是空值或垃圾值......然后你可以从那里追溯以找出这是如何发生的。

于 2013-01-25T17:49:09.217 回答