ABC
ABC

Reputation: 19

Modifying data in a Linked List in C?

When I modify the linked list (based on the ID), it modifies the node successfully but deletes the rest of the list. The whole list stays only if I modify the most recent node that I've added to the list.

I know that the problem is at the end where it says:

     phead=i;

     return phead;

But I don't know how to fix it as I haven't found anything to help me, even though I'm sure it is simple to know why it is wrong.

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;

}

When I return it, i say head=modify1Item(phead);

Upvotes: 0

Views: 305

Answers (1)

Kevin Ng
Kevin Ng

Reputation: 2174

I tested your code everything worked as expected. Without seeing your code, I can't comment much. But I think, for your code, the only time everything would get delete is if you assign the return value incorrectly. So this below is probably something close to your code. For the test code below, unless you modify it, the IDs are 0, 1, and 2. Oh and the reason why I commented only work for 0 to 9 is because I don't want to make up the entire char string so I used i ^ 48. Of which, 0 - 9 ^ 48 would turn into the correspondent ASCII code of 0 - 9. If you go beyond that, you may get weird result for that two string that all.

I just noticed that you use NULL in your search. Thus, I updated the code so the "next" of last index will be NULL otherwise if your code found nothing, it will run forever.

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

Upvotes: 1

Related Questions