fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct node *create_node(int);
  6. struct node *add_node(struct node *, int);
  7. void asce_order(struct node *);
  8. void desc_order(struct node *);
  9.  
  10. struct node
  11. {
  12. int data;
  13. int count;
  14. struct node *next, *previous;
  15. };
  16.  
  17. struct node *create_node(int value)
  18. {
  19. struct node *pnode = malloc(sizeof(struct node));
  20. pnode->data = value;
  21. pnode->count = 1;
  22. pnode->next = pnode->previous = NULL;
  23. return pnode;
  24. }
  25.  
  26. struct node *add_node(struct node *pnode, int value)
  27. {
  28. if (pnode == NULL)
  29. {
  30. pnode = create_node(value);
  31. return pnode;
  32. }
  33. else if (pnode->data == value)
  34. {
  35. pnode->count++;
  36. return pnode;
  37. }
  38.  
  39. if (pnode->data > value)
  40. {
  41. if (pnode->previous == NULL)
  42. {
  43. return pnode->previous = create_node(value);
  44. }
  45. return add_node(pnode->previous, value);
  46. }
  47. else
  48. {
  49. if (pnode->next == NULL)
  50. {
  51. return pnode->next = create_node(value);
  52. }
  53. return add_node(pnode->next, value);
  54. }
  55. }
  56.  
  57. void asce_order(struct node *pnode)
  58. {
  59. int i;
  60.  
  61. if (pnode->previous != NULL)
  62. {
  63. asce_order(pnode->previous);
  64. }
  65.  
  66. for(i = 0; i < pnode->count; i++)
  67. {
  68. printf("%d\n", pnode->data);
  69. }
  70.  
  71. if(pnode->next != NULL)
  72. {
  73. asce_order(pnode->next);
  74. }
  75. }
  76.  
  77. void desc_order(struct node *pnode)
  78. {
  79. int i;
  80.  
  81. if (pnode->next != NULL)
  82. {
  83. desc_order(pnode->next);
  84. }
  85.  
  86. for (i = 0; i < pnode->count; i++)
  87. {
  88. printf("%d\n", pnode->data);
  89. }
  90.  
  91. if (pnode->previous != NULL)
  92. {
  93. desc_order(pnode->previous);
  94. }
  95. }
  96.  
  97. void free_variables(struct node *pnode)
  98. {
  99. if (pnode == NULL)
  100. {
  101. return;
  102. }
  103.  
  104. if (pnode->next != NULL)
  105. {
  106. free_variables(pnode->next);
  107. }
  108.  
  109. if (pnode->previous != NULL)
  110. {
  111. free_variables(pnode->previous);
  112. }
  113.  
  114. free(pnode);
  115. }
  116.  
  117. int main(void)
  118. {
  119. struct node *head=NULL;
  120.  
  121. head = add_node(head, 2);
  122. add_node(head, 0);
  123. add_node(head, 6);
  124. add_node(head, 7);
  125. add_node(head, 4);
  126. add_node(head, 2);
  127. add_node(head, 8);
  128. add_node(head, 3);
  129. add_node(head, 7);
  130. add_node(head, 5);
  131. add_node(head, 0);
  132. add_node(head, 1);
  133. add_node(head, 6);
  134. add_node(head, 9);
  135.  
  136. printf("ascending order:\n");
  137. asce_order(head);
  138.  
  139. printf("descending order:\n");
  140. desc_order(head);
  141.  
  142. return 0;
  143. }
  144.  
Success #stdin #stdout 0.02s 1808KB
stdin
Standard input is empty
stdout
ascending order:
0
0
1
2
2
3
4
5
6
6
7
7
8
9
descending order:
9
8
7
7
6
6
5
4
3
2
2
1
0
0