当我修改链接列表(基于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);