Advertisement
Farid_Mia59

mgll

Nov 2nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. // A structure representing a node of a linked list.
  6. struct node
  7. {
  8. int data;
  9. node *next;
  10. };
  11.  
  12. // A function creating a new node.
  13. node* NewNode(int d)
  14. {
  15. struct node *temp = new node;
  16. temp->data = d;
  17. temp->next = NULL;
  18. // Returning temp as the new node.
  19. return temp;
  20. }
  21.  
  22. // A function adding the given data at the end of the linked list.
  23. node* AddToList(node *tail, int data)
  24. {
  25. struct node *newnode;
  26. newnode = NewNode(data);
  27.  
  28. if(tail == NULL)
  29. {
  30. tail = newnode;
  31. }
  32. // If tail is not null assign newnode to the next of tail.
  33. else
  34. {
  35. tail->next = newnode;
  36. // Shift tail pointer to the added node.
  37. tail = tail->next;
  38. }
  39.  
  40. return tail;
  41. }
  42.  
  43. node* Merge(node* h1, node* h2)
  44. {
  45. node *t1 = new node;
  46. node *t2 = new node;
  47. node *temp = new node;
  48.  
  49. // Return if the first list is empty.
  50. if(h1 == NULL)
  51. return h2;
  52.  
  53. // Return if the Second list is empty.
  54. if(h2 == NULL)
  55. return h1;
  56.  
  57. t1 = h1;
  58.  
  59. // A loop to traverse the second list, to merge the nodes to h1 in sorted way.
  60. while (h2 != NULL)
  61. {
  62. // Taking head node of second list as t2.
  63. t2 = h2;
  64.  
  65. // Shifting second list head to the next.
  66. h2 = h2->next;
  67. t2->next = NULL;
  68.  
  69. // If the data value is lesser than the head of first list add that node at the beginning.
  70. if(h1->data > t2->data)
  71. {
  72. t2->next = h1;
  73. h1 = t2;
  74. t1 = h1;
  75. continue;
  76. }
  77.  
  78. // Traverse the first list.
  79. flag:
  80. if(t1->next == NULL)
  81. {
  82. t1->next = t2;
  83. t1 = t1->next;
  84. }
  85. // Traverse first list until t2->data more than node's data.
  86. else if((t1->next)->data <= t2->data)
  87. {
  88. t1 = t1->next;
  89. goto flag;
  90. }
  91. else
  92. {
  93. // Insert the node as t2->data is lesser than the next node.
  94. temp = t1->next;
  95. t1->next = t2;
  96. t2->next = temp;
  97. }
  98. }
  99.  
  100. // Return the head of new sorted list.
  101. return h1;
  102. }
  103.  
  104.  
  105. // A function implementing Merge Sort on linked list using reference.
  106. void MergeSort(node **head)
  107. {
  108. node *first = new node;
  109. node *second = new node;
  110. node *temp = new node;
  111. first = *head;
  112. temp = *head;
  113.  
  114. // Return if list have less than two nodes.
  115. if(first == NULL || first->next == NULL)
  116. {
  117. return;
  118. }
  119. else
  120. {
  121. // Break the list into two half as first and second as head of list.
  122. while(first->next != NULL)
  123. {
  124. first = first->next;
  125. if(first->next != NULL)
  126. {
  127. temp = temp->next;
  128. first = first->next;
  129. }
  130. }
  131. second = temp->next;
  132. temp->next = NULL;
  133. first = *head;
  134. }
  135.  
  136. // Implementing divide and conquer approach.
  137. MergeSort(&first);
  138. MergeSort(&second);
  139.  
  140. // Merge the two part of the list into a sorted one.
  141. *head = Merge(first, second);
  142. }
  143.  
  144. int main()
  145. {
  146. int n, i, num;
  147. struct node *head = new node;
  148. struct node *tail = new node;
  149. head = NULL;
  150. tail = NULL;
  151. cout<<"\nEnter the number of data element to be sorted: ";
  152. cin>>n;
  153.  
  154.  
  155. // Create linked list.
  156. for(i = 0; i < n; i++)
  157. {
  158. cout<<"Enter element "<<i+1<<": ";
  159. cin>>num;
  160.  
  161. tail = AddToList(tail, num);
  162. if(head == NULL)
  163. head = tail;
  164. }
  165.  
  166. // Send reference of head into MergeSort().
  167. MergeSort(&head);
  168.  
  169. // Printing the sorted data.
  170. cout<<"\nSorted Data ";
  171.  
  172. while(head != NULL)
  173. {
  174. cout<<".."<<head->data;
  175. head=head->next;
  176. }
  177. return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement