fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct node {
  6. int value;
  7. struct node * next;
  8. } NODE;
  9.  
  10. void nodecreate( struct node * tail, int n ) {
  11. tail->next = malloc( sizeof * tail->next );
  12. tail->next->value = n;
  13. tail->next->next = NULL;
  14. }
  15.  
  16. struct node * listappend( struct node * list, int n ) {
  17. struct node * tail = list;
  18. while ( tail->next != NULL ) {
  19. tail = tail->next;
  20. }
  21. nodecreate( tail, n );
  22. return list;
  23. }
  24.  
  25. void listprint( struct node * list ) {
  26. int i = 0;
  27. while ( list != NULL ) {
  28. printf( "%d: %d\n", i, list->value );
  29. i++;
  30. list = list->next;
  31. }
  32. }
  33.  
  34. struct node * listdelete( struct node * list, int n ) {
  35. if ( list == NULL )
  36. return list;
  37.  
  38. struct node * caret = list;
  39. if ( list->value == n ) {
  40. list = list->next;
  41. free( caret );
  42. }
  43. else {
  44. while ( caret->next != NULL && caret->next->value != n ) {
  45. caret = caret->next;
  46. }
  47. while ( caret->next->next != NULL ) {
  48. caret->next->value = caret->next->next->value;
  49. caret = caret->next;
  50. }
  51. free( caret->next );
  52. caret->next = NULL;
  53. }
  54.  
  55. return list;
  56. }
  57.  
  58. int main( ) {
  59. struct node list = {
  60. .value = 0,
  61. .next = NULL
  62. };
  63.  
  64. for ( int i = 1; i < 5; i++ ) {
  65. listappend( &list, i * 100 );
  66. }
  67.  
  68. listprint( &list );
  69. listdelete( &list, 200 );
  70. listprint( &list );
  71.  
  72. return 0;
  73. }
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
0: 0
1: 100
2: 200
3: 300
4: 400
0: 0
1: 100
2: 300
3: 400