Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <xlocale.h>
  4. #include "linkedlist.h"
  5.  
  6. sList* create_sList(void) {
  7. sList *lisy = malloc(sizeof (sList));
  8. lisy->count = 0;
  9. lisy->first = NULL;
  10. lisy->last = NULL;
  11. return lisy;
  12. }
  13.  
  14. int insert_element_s(sList *L, snodeType *p, int value) {
  15. snodeType *head = malloc(sizeof (snodeType));
  16. if (p == NULL) {
  17. snodeType *tep = L->first;
  18. L->first = head;
  19. head->next = tep;
  20. head->value = value;
  21. L->count++;
  22. if (L->count == 1) {
  23. L->last = head;
  24. L->last->next = NULL;
  25. }
  26. } else {
  27. snodeType *t = malloc(sizeof (snodeType));
  28. t = L->first;
  29. while (t != p) {
  30. t = t->next;
  31. }
  32. head->value = value;
  33. head->next = t->next;
  34. t->next = head;
  35. L->count++;
  36. if (head->next == NULL) {
  37. L->last = head;
  38. L->last->next = NULL;
  39. }
  40. }
  41. return 0;
  42. }
  43.  
  44. int delete_element_s(sList *L, snodeType *p) {
  45. int i = 1;
  46. snodeType *t = malloc(sizeof (snodeType));
  47. if (L != NULL) {
  48. if (p == NULL) {
  49. t = L->first;
  50. if (t != NULL) {
  51. L->first = t->next;
  52. L->count--;
  53. if (t->next == NULL) {
  54. L->last = NULL;
  55. }
  56. i = 0;
  57. }
  58. } else {
  59. t = p;
  60. if (t->next != NULL) {
  61. t = t->next;
  62. p->next = t->next;
  63. L->count--;
  64. if (t->next == NULL) {
  65. L->last = p;
  66. }
  67. i = 0;
  68. }
  69. }
  70. }
  71. return i;
  72. }
  73.  
  74. sList* merge_lists(sList *L1, sList *L2) {
  75. sList *head = create_sList();
  76. if (L1->count == 0 && L2->count == 0) {
  77. return head;
  78. }
  79. snodeType *t1 = malloc(sizeof (snodeType));
  80. snodeType *t2 = malloc(sizeof (snodeType));
  81. snodeType *last = malloc(sizeof (snodeType));
  82. if (L1->count == 0) {
  83. insert_element_s(head, NULL, L2->first->value);
  84. t1 = NULL;
  85. if (L2->count == 1) {
  86. return head;
  87. } else {
  88. t2 = L2->first->next;
  89. }
  90. last = L2->first;
  91. } else if (L2->count == 0) {
  92. insert_element_s(head, NULL, L1->first->value);
  93. if (L1->count == 1) {
  94. return head;
  95. } else {
  96. t1 = L1->first->next;
  97. }
  98. t2 = NULL;
  99. last = L1->first;
  100. } else if (L1->first->value < L2->first->value) {
  101. insert_element_s(head, NULL, L1->first->value);
  102. t1 = L1->first->next;
  103. t2 = L2->first;
  104. last = L1->first;
  105. } else if (L1->first->value > L2->first->value) {
  106. insert_element_s(head, NULL, L2->first->value);
  107. t1 = L1->first;
  108. t2 = L2->first->next;
  109. last = L2->first;
  110. } else if (L1->first->value == L2->first->value) {
  111. insert_element_s(head, NULL, L2->first->value);
  112. t1 = L1->first->next;
  113. t2 = L2->first->next;
  114. last = L2->first;
  115. }
  116. while (t1 != NULL || t2 != NULL) {
  117. if (t2 == NULL) {
  118. insert_element_s(head, last, t1->value);
  119. last = t1;
  120. t1 = t1->next;
  121. } else if (t1 == NULL) {
  122. insert_element_s(head, last, t2->value);
  123. last = t2;
  124. t2 = t2->next;
  125. } else if (t1->value < t2->value) {
  126. insert_element_s(head, last, t1->value);
  127. last = t1;
  128. t1 = t1->next;
  129. } else if (t1->value > t2->value) {
  130. insert_element_s(head, last, t2->value);
  131. last = t2;
  132. t2 = t2->next;
  133. } else if (t1->value == t2->value) {
  134. insert_element_s(head, last, t2->value);
  135. last = t2;
  136. t2 = t2->next;
  137. t1 = t1->next;
  138. }
  139. }
  140.  
  141.  
  142. return head;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement