c - 修改C中链表中的数据?

标签 c pointers linked-list nodes

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

最佳答案

我测试了你的代码,一切都按预期工作。没有看到你的代码,我无法发表太多评论。但我认为,对于您的代码,唯一一次所有内容都会被删除是如果您错误地分配了返回值。所以下面的代码可能与您的代码很接近。对于下面的测试代码,除非您修改它,否则 ID 为 0、1 和 2。哦,我注释仅适用于 0 到 9 的原因是因为我不想组成整个 char 字符串,所以我使用了 i ^ 48。其中 0 - 9 ^ 48 会变成对应的 ASCII 码 0 - 9。如果超出这个范围,你可能会得到这两个字符串的奇怪结果。

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

#include <stdio.h>
#include <string.h>

typedef struct ItemNode {
  int ID;
  int uPrice;
  int onHand;
  char name[30];
  char desc[30];
  struct ItemNode * next;
} ItemNode ;

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;
}

int main(){
  // only work for 0  - 9.
  int index = 3;
  ItemNode iArr[index];

  for ( int i = 0; i < index; i++ ){
    iArr[i].ID = i;
    iArr[i].uPrice = i + i;
    iArr[i].onHand = i * i;
    iArr[i].name[0] = i ^ 48;
    iArr[i].desc[0] = i ^ 48;

    // If last index link back to first index.
    // Updated: but for you usage case 
    // because of your search function
    // last index should be NULL otherwise your 
    // search will run forever
    if ( i < index - 1 ) iArr[i].next = &iArr[i + 1];
    else iArr[i].next = NULL; // if change search method with unique ID then you can use -> &iArr[0];
  }

  // Mod 0
  ItemNode * test = modify1Item(iArr);
  printf("0 name: %s\n\n",iArr[0].name );

  // Mod 1
  ItemNode * test1 = modify1Item(iArr);
  printf("1 name: %s\n\n",iArr[1].name );

  // Mod 2
  ItemNode * test2 = modify1Item(iArr);
  printf("2 name: %s\n\n",iArr[2].name );

  // Check if 0 is still there.
  printf("0 name: %s\n\n",iArr[0].name );

  return 0;
}

关于c - 修改C中链表中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59061186/

相关文章:

c - 在 C 中使用指针和数组

c++ - 如何在链表中生成唯一ID?

sql - 如何在不遍历SQLite3的情况下从SQL表中获取最后一列的名称?

c - 在 C 中使用 open() 函数创建的文件的默认访问模式是什么?

C、从文本文件中读取 double 值

c - 这段代码保存了关于中断线程的哪些信息?

c - 这里的负指针值是多少?

c - 我的代码有什么问题(使用指针复制字符串?)?

c++ - 双循环链表。新节点未插入。 C++

c - 在链接列表中查找循环(​​获取段错误)