开发者

How to EDIT a node? and How to DELETE a modified node?

开发者 https://www.devze.com 2023-02-14 20:42 出处:网络
How to edit a node ? I used recipe_id to be the primary key .. In my edit section i need to search first for the recipe_id, if it exists the program will let the user to edit the recipe_id she inputt

How to edit a node ?

I used recipe_id to be the primary key .. In my edit section i need to search first for the recipe_id, if it exists the program will let the user to edit the recipe_id she inputted. I don't know how to do it.

How to delete a node?

just like my question above, i used recipe_id to modify which node will going to be ed开发者_运维知识库ited and deleted... please help me..

another thing is i don't know how to display all the data in my linked list without any run time error.. - _ -

thanks! :)) -maan :D


Sounds like you are using a linked list as a recipe database of sorts. Editing a node should be simple. Just run through the list until you find the matching recipe_id, then edit the fields with the new user input.

NODE *tmp = list;
while(tmp && tmp->recipe_id != recipe_id_to_edit)
  tmp = tmp->next;
if(tmp) {
  /*  edit node here  */
}

Deleting a node can be done like this.

NODE *tmp = list;
if(tmp) {
  if(tmp->recipe_id == recipe_id_to_delete) {
    list = list->next;
    free(tmp);
  }
  else {
    while(tmp->next && tmp->next->recipe_id != recipe_id_to_delete)
      tmp = tmp->next;
        if(tmp->next) {
      NODE *to_free = tmp->next;
      tmp->next = tmp->next->next;
      free(to_free);
    }
  }      
}

Displaying data in the list at runtime should be as simple as

NODE *tmp = list;
while(tmp) {
  printf("%s %s\n", tmp->field1, tmp->field2 );
  tmp = tmp->next
}

This is assuming that your list is singly linked and properly NULL terminated.

0

精彩评论

暂无评论...
验证码 换一张
取 消