Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef struct lnode
  5. {
  6. int data;
  7. struct lnode* next;
  8. }LNODE;
  9.  
  10. typedef struct list
  11. {
  12. LNODE* head;
  13. LNODE* tail;
  14. }LIST;
  15.  
  16.  
  17. LIST mergeA(LIST lst1, LIST lst2);
  18. LIST mergeB(LIST lst1, LIST lst2);
  19. LIST mergeC(LIST lst1, LIST lst2);
  20. void makeEmptyList(LIST &newList);
  21. void insertDataToEndList(LIST &lst, int data);
  22. LNODE* createNewListNode(int data, LNODE* next);
  23. void insertNodeToEndList(LIST &lst, LNODE* newTail);
  24. bool isEmptyList(LIST lst);
  25. void printList(LIST lst);
  26.  
  27.  
  28. void main()
  29. {
  30. {
  31. LIST lst1, lst2, newList;
  32.  
  33. makeEmptyList(lst1);
  34. makeEmptyList(lst2);
  35.  
  36. insertDataToEndList(lst1, 2);
  37. insertDataToEndList(lst1, 5);
  38. insertDataToEndList(lst1, 7);
  39. insertDataToEndList(lst1, 11);
  40.  
  41. insertDataToEndList(lst2, 1);
  42. insertDataToEndList(lst2, 6);
  43. insertDataToEndList(lst2, 9);
  44.  
  45. newList = mergeA(lst1, lst2);
  46. printList(newList);
  47.  
  48. cout << endl << endl;
  49.  
  50. system("pause");
  51. }
  52. {
  53. LIST lst1, lst2, newList;
  54.  
  55. makeEmptyList(lst1);
  56. makeEmptyList(lst2);
  57.  
  58. insertDataToEndList(lst1, 2);
  59. insertDataToEndList(lst1, 5);
  60. insertDataToEndList(lst1, 7);
  61. insertDataToEndList(lst1, 11);
  62.  
  63. insertDataToEndList(lst2, 1);
  64. insertDataToEndList(lst2, 6);
  65. insertDataToEndList(lst2, 9);
  66.  
  67. newList = mergeB(lst1, lst2);
  68. if (isEmptyList(newList))
  69. {
  70. cout << "is empty list" << endl;
  71. }
  72. else
  73. {
  74. printList(newList);
  75. }
  76. cout << endl << endl;
  77.  
  78. system("pause");
  79. }
  80.  
  81. }
  82.  
  83.  
  84.  
  85.  
  86. LIST mergeA(LIST lst1, LIST lst2)
  87. {
  88. LNODE* curr1 = lst1.head;
  89. LNODE* curr2 = lst2.head;
  90.  
  91. LIST newList;
  92. makeEmptyList(newList);
  93.  
  94. while (curr1 != NULL && curr2 != NULL)
  95. {
  96. if (curr1->data < curr2->data)
  97. {
  98. insertDataToEndList(newList, curr1->data);
  99. curr1 = curr1->next;
  100. }
  101. else
  102. {
  103. insertDataToEndList(newList, curr2->data);
  104. curr2 = curr2->next;
  105. }
  106. }
  107.  
  108. while (curr1 != NULL)
  109. {
  110. insertDataToEndList(newList, curr1->data);
  111. curr1 = curr1->next;
  112. }
  113.  
  114. while (curr2 != NULL)
  115. {
  116. insertDataToEndList(newList, curr2->data);
  117. curr2 = curr2->next;
  118. }
  119.  
  120. return newList;
  121. }
  122. /*
  123. 1 2 5 6 7 9 11
  124.  
  125. Press any key to continue . . .
  126.  
  127. */
  128.  
  129.  
  130. LIST mergeC(LIST lst1, LIST lst2)
  131. {
  132. LIST newList;
  133. makeEmptyList(newList);
  134.  
  135. if (isEmptyList(lst1) && !isEmptyList(lst2))
  136. {
  137. newList = lst2;
  138. return newList;
  139. }
  140. else if (!isEmptyList(lst1) && isEmptyList(lst2))
  141. {
  142. newList = lst1;
  143. return newList;
  144. }
  145. else if (isEmptyList(lst1) && isEmptyList(lst2))
  146. {
  147. return newList;
  148. }
  149. else
  150. {
  151. if (lst1.head->data < lst2.head->data)
  152. {
  153. insertDataToEndList(newList, lst1.head->data);
  154. lst1.head = lst1.head->next;
  155. mergeC(lst1, lst2);
  156. }
  157. else (lst2.head->data < lst1.head->data);
  158. {
  159. insertDataToEndList(newList, lst2.head->data);
  160. lst2.head = lst2.head->next;
  161. mergeC(lst1, lst2);
  162. }
  163. }
  164. }
  165.  
  166.  
  167. void makeEmptyList(LIST &lst)
  168. {
  169. lst.head = lst.tail = NULL;
  170. }
  171.  
  172. void insertDataToEndList(LIST &lst, int data)
  173. {
  174. LNODE* newTail;
  175. newTail = createNewListNode(data, NULL);
  176. insertNodeToEndList(lst, newTail);
  177. }
  178.  
  179.  
  180. LNODE* createNewListNode(int data, LNODE* next)
  181. {
  182. LNODE* res;
  183. res = new LNODE;
  184. res->data = data;
  185. res->next = next;
  186. return res;
  187. }
  188.  
  189. void insertNodeToEndList(LIST &lst, LNODE* newTail)
  190. {
  191. if (isEmptyList(lst))
  192. lst.head = lst.tail = newTail;
  193. else
  194. {
  195. lst.tail->next = newTail;
  196. lst.tail = newTail;
  197. }
  198. lst.tail->next = NULL;
  199. }
  200.  
  201. bool isEmptyList(LIST lst)
  202. {
  203. if (lst.head == NULL)
  204. return true;
  205. else
  206. return false;
  207. }
  208.  
  209. void printList(LIST lst)
  210. {
  211. LNODE *curr = lst.head;
  212.  
  213. while (curr != NULL)
  214. {
  215. cout << curr->data << " ";
  216. curr = curr->next;
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement