The structure for the list is set. How to delete a user-defined item in this list? Code does not work correctly - deletes all list items.

delete_3(Person *lst){ int num = 0; printf("Какой элемент удалять?\n"); scanf("%d",&num); Person *p; p = lst; do { p->name[0] = '\0'; p->job[0] = '\0'; p->home[0] = '\0'; print_2(lst); p = p->ptr; } while (p != NULL); } 

Closed due to the fact that off-topic participants Kromster , freim , LFC , aleksandr barakin , 0xdb 10 Apr at 17:42 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, freim, LFC, 0xdb
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Questions asking for help with debugging (“why does this code not work?”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question. Questions without an explicit description of the problem are useless for other visitors. - Kromster
  • Can you help? - YailGafu
  • one
    In order to help you, you must edit your question according to my message above. - Kromster
  • Done! ....... - YailGafu
  • And why do you enter num , if further this variable is not used in any way? - Mikhail Murugov

1 answer 1

You overwrite every line, every element in the loop, and you just need to throw a pointer to the next element.

 delete_3(Person *lst){ int num, i = 0; printf("Какой элемент удалять?\n"); scanf("%d",&num); Person *p; p = lst; while (p != NULL && i < num){ p = p->ptr; ++i } if(p != NULL){ if(num == 0){ lst = p->ptr; } else if(p->ptr != NULL){ p->ptr = p->ptr->ptr; } else { p->ptr = NULL; } } print_2(lst); } 
  • Suppose the list is: Mikhail Maria Alia Ivan After executing the code several times, I will delete the elements from the bottom, and not the specified line - YailGafu
  • I can’t delete just the specified line in the list - YailGafu
  • In the sense that if you run the code several times, then the elements will be deleted from the bottom? Well, yes, if you delete 2 lines 2 times, then 2 elements will be removed, but not one. - Komdosh
  • corrected the way the removal of the first element - Komdosh
  • Thank you very much!!! - YailGafu