fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4.  
  5.  
  6. /* struct */
  7.  
  8. typedef struct node {
  9. int value;
  10. struct node *next;
  11. } NODE;
  12.  
  13.  
  14. NODE *create_node(int value)
  15. {
  16. NODE *new;
  17.  
  18. if (!(new =malloc(sizeof(NODE)))) return NULL;
  19. new->value = value;
  20. new->next= NULL;
  21.  
  22. return new;
  23. }
  24.  
  25. NODE *append_node(NODE *head, int value)
  26. {
  27. NODE *new;
  28. NODE *current_node = head;
  29. NODE *anterior_node = NULL;
  30.  
  31. if((new = create_node(value)) == NULL) return NULL;
  32.  
  33. while(current_node != NULL && current_node->value < value)
  34. {
  35. anterior_node = current_node;
  36. current_node = current_node->next;
  37. }
  38.  
  39. if (anterior_node == NULL)
  40. {
  41. new->next = head;
  42. head = new;
  43. }
  44. else
  45. {
  46. new->next = current_node;
  47. anterior_node->next = new;
  48. }
  49. }
  50.  
  51. void print_list(NODE *head)
  52. {
  53. NODE *current_node = head;
  54. while(current_node != NULL){
  55. printf("%d\n",current_node->value);
  56. current_node = current_node->next;
  57. }
  58. }
  59.  
  60.  
  61. int main(void)
  62. {
  63. NODE *head = create_node(10);
  64.  
  65.  
  66. append_node(head, 20);
  67. print_list(head);
  68. return 0;
  69. }
Success #stdin #stdout 0s 1920KB
stdin
Standard input is empty
stdout
10
20