修改C中的链接列表中的数据吗?

当我修改链接列表(基于ID)时,它成功修改了节点,但删除了列表的其余部分。仅当我修改添加到列表中的最新节点时,整个列表才会保留。

我知道问题出在问题的最后:

     phead=i;

     return phead;

但是我不知道如何解决它,因为我没有找到任何可以帮助我的东西,即使我确信知道为什么会出错很简单。

struct ItemNode *modify1Item(struct ItemNode *phead){  

int modID;
int lfound=0;
int lID;
char lDesc[30];
char lName[30];
double lUPrice;
int lOnHand;
struct ItemNode *i=phead;

printf("Enter the ID of the item that you want to modify\n");

scanf("%d",&modID);

while(i != NULL){

    if(i->ID == modID){
        break;
    }

    i= i->next;
}


if(i==NULL){

    printf("An item with that ID wasn't found.\n"); 
    return 0;

 }

else{ 


    printf("Enter new Name\n");

    scanf("%s",lName);

    strcpy(i->name,lName); 

    printf("Enter new Description\n");

    scanf("%s",lDesc);

    strcpy(i->desc,lDesc); 

    printf("Enter new Unit Price $\n");

    scanf("%lf",&lUPrice);

    i->uPrice = lUPrice;

    printf("Enter new Number of Items On Hand\n");

    scanf("%d",&lOnHand);

    i->onHand = lOnHand;

   }

phead=i;

return phead;

}

当我返回它时,我说head = modify1Item(phead);

chz_zc 回答:修改C中的链接列表中的数据吗?

我测试了您的代码,一切正常。没有看到您的代码,我无法发表过多评论。但是我认为,对于您的代码,唯一一次删除所有内容的方法就是错误地分配了返回值。因此,以下内容可能与您的代码很接近。对于下面的测试代码,除非您对其进行修改,否则它们的ID分别为0、1和2。哦,我之所以只注释0到9,是因为我不想组成整个char字符串,所以我使用了i ^48。其中0-9 ^ 48会变成对应的ASCII码0-9。如果超出该范围,则可能会得到两个字符串的奇怪结果。

我刚刚注意到您在搜索中使用NULL。因此,我更新了代码,以使最后一个索引的“下一个”为NULL,否则,如果您的代码未找到任何内容,它将永远运行。

acquireConnectionTimeout
本文链接:https://www.f2er.com/3025161.html

大家都在问