fork download
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <assert.h>
  5.  
  6. typedef struct node_t {
  7. int x;
  8. struct node_t *next;
  9. } *Node;
  10.  
  11. static int advance(Node *nd)
  12. {
  13. int res = (*nd)->x;
  14.  
  15. *nd=(*nd)->next;
  16.  
  17. return res;
  18. }
  19.  
  20. int getMin(Node *list1, Node *list2)
  21. {
  22. assert(*list1 || *list2);
  23.  
  24. if (*list1 == NULL) return advance(list2);
  25. if (*list2 == NULL) return advance(list1);
  26. if ((*list1)->x < (*list2)->x) return advance(list1);
  27.  
  28. return advance(list2);
  29. }
  30.  
  31. int main(void)
  32. {
  33. struct node_t a3 = {13, NULL};
  34. struct node_t a2 = {12, &a3};
  35. struct node_t a1 = {4, &a2};
  36. struct node_t a0 = {1, &a1};
  37. Node a = &a0;
  38.  
  39. struct node_t b3 = {7, NULL};
  40. struct node_t b2 = {5, &b3};
  41. struct node_t b1 = {3, &b2};
  42. struct node_t b0 = {2, &b1};
  43. Node b = &b0;
  44.  
  45. while (a || b) {
  46. printf("%d\n", getMin(&a, &b));
  47. }
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 4264KB
stdin
Standard input is empty
stdout
1
2
3
4
5
7
12
13