fork(4) download
  1.  
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<limits.h>
  6.  
  7. struct node
  8. {
  9. int value;
  10. node *next;
  11. };
  12.  
  13. struct Linked
  14. {
  15. node *Head;
  16. int count1;
  17. };
  18.  
  19. void Insert_Function(Linked *Obj,int ele)
  20. {
  21. node *temp=(node *)malloc(sizeof(node));
  22. temp->value=ele;
  23. temp->next=Obj->Head;
  24. Obj->Head=temp;
  25. Obj->count1++;
  26. }
  27.  
  28. void Display(Linked *Obj)
  29. {
  30. node *temp=Obj->Head;
  31. if(temp==NULL)
  32. {
  33. printf("\n Linked list is empty");
  34. return;
  35. }
  36. while(temp!=NULL)
  37. {
  38. printf("%d ",temp->value);
  39. temp=temp->next;
  40. }
  41. }
  42.  
  43.  
  44. void Reverse(Linked *Obj)
  45. {
  46. node *prev=NULL,*mid=NULL,*first=Obj->Head;
  47. while(first!=NULL)
  48. {
  49. mid=first;
  50. first=first->next;
  51. mid->next=prev;
  52. prev=mid;
  53. }
  54. Obj->Head=mid;
  55. }
  56.  
  57.  
  58.  
  59. node *Mid_Find(node *temp)
  60. {
  61. node *slow,*fast;
  62. slow=temp;
  63. if(slow==NULL)
  64. return(slow);
  65. fast=slow->next;
  66. if(fast==NULL)
  67. return(slow);
  68. while(fast&&fast->next)
  69. {
  70. slow=slow->next;
  71. fast=fast->next->next;
  72. }
  73. return(slow);
  74. }
  75.  
  76.  
  77. node *Merge_Lists(node *first,node *second)
  78. {
  79. int val1,val2;
  80. Linked *result=(Linked *)malloc(sizeof(Linked));
  81. result->Head=NULL;
  82. result->count1=0;
  83. while(first!=NULL||second!=NULL)
  84. {
  85. if(first!=NULL)
  86. val1=first->value;
  87. else
  88. val1=INT_MAX;
  89. if(second!=NULL)
  90. val2=second->value;
  91. else
  92. val2=INT_MAX;
  93. if(val1<val2)
  94. {
  95. Insert_Function(result,val1);
  96. if(first!=NULL)
  97. first=first->next;
  98. }
  99. else
  100. {
  101. Insert_Function(result,val2);
  102. if(second!=NULL)
  103. second=second->next;
  104. }
  105. }
  106. Reverse(result);
  107. return(result->Head);
  108. }
  109.  
  110.  
  111. node * Merge_Sort(node *temp)
  112. {
  113. node *first,*second,*mid,*result=NULL;
  114.  
  115. if(temp->next==NULL)
  116. return(temp);
  117. mid=Mid_Find(temp);
  118.  
  119. first=temp;
  120. if(mid!=NULL)
  121. second=mid->next;
  122. mid->next=NULL;
  123. first=Merge_Sort(first);
  124. second=Merge_Sort(second);
  125. result=Merge_Lists(first,second);
  126. return(result);
  127. }
  128.  
  129.  
  130. int main()
  131. {
  132.  
  133. Linked *Obj=(Linked *)malloc(sizeof(Linked));
  134. bool check;
  135. Obj->Head=NULL;
  136. Obj->count1=0;
  137. Insert_Function(Obj,10);
  138. Insert_Function(Obj,5);
  139. Insert_Function(Obj,1);
  140. Insert_Function(Obj,7);
  141. Insert_Function(Obj,4);
  142. Insert_Function(Obj,2);
  143. Insert_Function(Obj,3);
  144. Insert_Function(Obj,9);
  145. Insert_Function(Obj,8);
  146. Insert_Function(Obj,6);
  147. printf("\n Linked List : " );
  148. Display(Obj);
  149. Obj->Head=Merge_Sort(Obj->Head);
  150. printf("\n Sorted Linked List is : ");
  151. Display(Obj);
  152. return(0);
  153. }
  154.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
 Linked List : 6 8 9 3 2 4 7 1 5 10 
 Sorted Linked List is : 1 2 3 4 5 6 7 8 9 10