fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct list {
  5. int val;
  6. struct list *next;
  7. };
  8.  
  9. void insert(struct list **pp, int val)
  10. {
  11. struct list *p;
  12.  
  13. p = malloc(sizeof (struct list));
  14. p->val = val;
  15. while (*pp) {
  16. if ((*pp)->val >= val) break;
  17. pp = &(*pp)->next;
  18. }
  19. p->next = *pp;
  20. *pp = p;
  21. }
  22.  
  23. int main()
  24. {
  25. struct list *p, *next, *head = NULL;
  26. int val;
  27.  
  28. while (1) {
  29. scanf("%d", &val);
  30. if (val < 0) break;
  31. insert(&head, val);
  32. }
  33. for (p = head; p; p = next) {
  34. printf("%d ", p->val);
  35. next = p->next;
  36. free(p);
  37. }
  38. printf("\n");
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.02s 1856KB
stdin
8 8 0 1 6 8 0 0 -1
stdout
0 0 0 1 6 8 8 8