gpt4 book ai didi

c - 通过循环获取链表的值

转载 作者:行者123 更新时间:2023-11-30 17:30:00 25 4
gpt4 key购买 nike

我必须创建一个函数,它可以获取采用双链表形式的矩阵的值。这是矩阵的结构

 typedef struct row {
unsigned int indiceRow;
struct row * next;
struct col * data;
} row;

typedef struct col{
double value;
unsigned int indiceColumn;
struct col * next;
} col;

typedef struct matrix{
int nRows;
int nCols;
struct row * rowFirst;
}matrix;

结构矩阵表示链表的顶部,包含行数和列数以及指向行节点列表的第一个节点的变量行。行节点包含矩阵的行号,称为下一个的变量行,表示矩阵的下一行,以及指向另一个列节点列表的变量数据。这些列节点包含列号、这些坐标(行、列)处的值以及接下来的列。只有不为零的值必须位于 col 链表中。

为了获取矩阵的精确点的值,我创建了函数 sp_get。它需要一个结构矩阵、我正在寻找的行和列以及一个双变量作为参数。它工作时返回 0,并用我正在寻找的值更新变量 double *val。

int sp_get( struct matrix *mat, unsigned int rows, unsigned int col, double *val){
row * temps = (row*)malloc(sizeof(row));
temps = mat->rowFirst;
while(temps->indiceRow!= rows){
temps = temps->next;
}

while(temps->data!= NULL && temps->data->indiceColumn!= col && temps->data->next!=NULL){
temps->data = temps->data->next;
}

if(temps->data->indiceColumn == col){
*(val) = temps->data->value;
}
else{
*(val) = 0.0;
}

return 0;

首先,我创建一个行变量来遍历矩阵,然后查找好的行,然后查找好的列。如果我找不到好的列,则意味着该值为 0。

当我使用该函数查找一个值时,它运行良好,并且始终返回正确的值。(tempMatrix 是一个矩阵变量并包含链接列表)

        double * vall =(double*)malloc(sizeof(double));
sp_get(tempMatrix, 2, 3, vall);

但是当我使用带有双循环的函数时,我没有得到相同的结果,我无法解释为什么......

double * vall =(double*)malloc(sizeof(double));
int i;
int j;
for(i=1;i<=tempMatrix->nRows;i++){
for(j=1; j<=tempMatrix->nCols;j++){
sp_get(tempMatrix,i,j,vall);
printf(" %f ", *(vall));
}
printf("\n");
}

Here are the result I get with the loops

and here are the results I should get

可能是内存泄漏的问题,不知道是哪里来的。

预先感谢您的帮助!

最佳答案

仅在 sp_get 中,以下问题就比比皆是:

记住前两行。

每当您在 C: 的连续行中看到类似的内容时:

ptr = malloc(...)
ptr = <something else>

总是内存泄漏。

更新列标题而不是简单地枚举它

找到所需的行后,您可以执行以下操作:

while(temps->data!= NULL && 
temps->data->indiceColumn!= col &&
temps->data->next!=NULL)
{
temps->data = temps->data->next;
}

问问自己,什么是 temps->data = ... 实际更新? 它正在更改 temps-> data 指针指向它自己的下一个,这意味着 temps->data 指向之前的内容已经消失了。如果 temps->data 是一个临时指针,那很好,但它不是。它是您在上一个循环中费尽心思找到的行结构中的 data 成员。

潜在的 NULL 指针取消引用

您可能会认为这样:

while(temps->data!= NULL && 
temps->data->indiceColumn!= col &&
temps->data->next!=NULL)

循环中的 while 条件将保证以下代码的 temp-data 为 NULL 的安全性:

if(temps->data->indiceColumn == col)
{
*(val) = temps->data->value;
}

但如果确实如此,那为什么还要费心第一个子句(顺便说一句,这是正确的)。添加最后一个子句 (temps->data->next!=NULL) 似乎是为了避免崩溃。这不是这样做的方法。

次要:隐藏带有参数 col 的类型 col

需要很少的解释。查看您的变量名称。

次要:使用时无需动态分配输出参数。

您的代码执行此操作:

double * vall =(double*)malloc(sizeof(double));
int i, j;
for(i=1;i<=tempMatrix->nRows;i++)
{
for(j=1; j<=tempMatrix->nCols;j++)
{
sp_get(tempMatrix,i,j,vall);
printf(" %f ", *(vall));
}
printf("\n");
}

可以轻松做到这一点:

double val = 0.0;
int i, j;
for(i=1;i<=tempMatrix->nRows;i++)
{
for(j=1; j<=tempMatrix->nCols;j++)
{
sp_get(tempMatrix,i,j,&val); // note address-of operator
printf(" %f ", val);
}
printf("\n");
}
<小时/>

更新了 sp_get

我很确定这就是您想要做的。如果找到并检索到索引值,则以下命令将返回 0,否则返回 -1 并且输出参数设置为 0.0。

int sp_get( struct matrix const *mat, unsigned int rows, unsigned int cols, double *val)
{
// prime to 0.0
*val = 0.0;
if (!mats)
return -1;

// walk the row table
struct row const *row_ptr = mat->rowFirst;
while (row_ptr && row_ptr->indiceRow != rows)
row_ptr = row_ptr->next;

// leave now if we didn't find the row.
if (!row_ptr)
return -1;

struct col const *col_ptr = row_ptr->data;
while (col_ptr && col_ptr->indiceColumn != cols)
col_ptr = col_ptr->next;

if (!col_ptr)
return -1;

*val = col_ptr->value;
return 0;
}

请注意,我们在实际矩阵中没有进行任何修改,因此整个矩阵,包括我们用于在其中进行索引的所有指针,都可以是 const(并且应该是)。

祝你好运。

关于c - 通过循环获取链表的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25361370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com