【问题标题】:Modifying data in a Linked List in C?在 C 中修改链表中的数据?
【发布时间】:2020-03-22 11:27:36
【问题描述】:

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

【问题讨论】:

  • 让我先提出一些建议。不要将您的链表管理功能与用户提供的输入操作混用。两者应保持分开。用户想要提供一些数据来对列表执行一些操作,该数据的获取和执行的操作应该是分开的。获取该数据,然后将其提供给您的链表函数以执行所需的操作。它显着整理了您的链表函数代码,并显着增加了在开发过程中隔离问题的机会。
  • 也就是说,这个函数只修改现有项目。这个函数的调用者,以及他们如何处理结果,将会很重要。我敢打赌它类似于 head = modifyItem(head); ,这是错误的,但这只是一个摇摆(wild-arse-guess),因为我们没有所需的minimal reproducible example
  • 今晚我的水晶球多云,但表明列表最初是在没有正确分配每个节点的情况下填充的,或者通过为每个节点分配相同的指针。我一直在摇球,但没有MCVE,它仍然是阴天。

标签: c pointers linked-list nodes


【解决方案1】:

我测试了您的代码,一切都按预期工作。没有看到你的代码,我不能发表太多评论。但我认为,对于您的代码,唯一会删除所有内容的情况是您错误地分配了返回值。因此,下面的内容可能与您的代码很接近。对于下面的测试代码,除非你修改它,否则 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;
}

【讨论】:

    猜你喜欢
    • 2012-04-02
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 2021-04-27
    相关资源
    最近更新 更多