hozer

Untitled

Jun 6th, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct _node {
  6. int data;
  7. struct _node *next, *prev;
  8. } node;
  9.  
  10.  
  11. node *addNode(node *head, node **tail, int num)
  12. {
  13. node * n = (node*) malloc(sizeof(node));
  14. n->next = NULL;
  15. n->prev = *tail;
  16. n->data = num;
  17. if (*tail)
  18. {
  19. (*tail)->next = n;
  20. }
  21. *tail = n;
  22. if (!head) head = *tail;
  23. return head;
  24. }
  25.  
  26.  
  27. node *dsort(node *head, node *tail)
  28. {
  29. node*el1 = head, *el2 = head;
  30.  
  31. while (el1){
  32. el2 = head;
  33. while (el2)
  34. {
  35. if (el1->data < el2->data){
  36. int tmp = el1->data;
  37. el1->data = el2->data;
  38. el2->data = tmp;
  39. }
  40. el2 = el2->next;
  41. }
  42. el1 = el1->next;
  43. }
  44. return head;
  45. }
  46.  
  47. node *put(node *headSource, node *head)
  48. {
  49. node *elStart = headSource;
  50.  
  51. while (elStart)
  52. {
  53. node *elf = head;
  54. if (elStart->data == elf->data)
  55. {
  56. while (elf && elStart->data != elf->data)
  57. {
  58. elStart = elStart->next;
  59. }
  60. node *el = head, *tmp = NULL;
  61. tmp = elStart->next;
  62. elStart->next = head;
  63. while (el->next){
  64. el = el->next;
  65. }
  66. tmp->prev = el;
  67. el->next = tmp;
  68.  
  69. }
  70. elStart = elStart->next;
  71. }
  72.  
  73. return headSource;
  74. }
  75.  
  76. void printList(node *head)
  77. {
  78. node *el = head;
  79. while (el)
  80. {
  81. printf("%d ", el->data);
  82. el = el->next;
  83. }
  84. }
  85.  
  86. int main()
  87. {
  88. int i, t;
  89. node *headS = NULL, *headTR = NULL, *headR = NULL, *tailS = NULL, *tailTR = NULL, *tailR = NULL;
  90.  
  91. printf("input L1: ");
  92. while (1)
  93. {
  94. scanf("%d", &t);
  95. if (t == 0) break;
  96. headS = addNode(headS, &tailS, t);
  97. }
  98.  
  99. printf("input L2: ");
  100. while (1)
  101. {
  102. scanf("%d", &t);
  103. if (t == 0) break;
  104. headTR = addNode(headTR, &tailTR, t);
  105. }
  106.  
  107. headS = dsort(headS, tailS);
  108. headS = put(headS, headTR);
  109. printList(headS);
  110.  
  111. _getch();
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment