Advertisement
Guest User

iuly

a guest
Apr 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. template<typename T> struct point {
  8. T x;
  9. T y;
  10. struct point<T> *next, *prev;
  11. };
  12.  
  13. template <typename T> class LinkedList {
  14. public:
  15. struct point<T> *pfirst, *plast;
  16.  
  17. void addFirst(T x, T y) {
  18. struct point<T> *paux;
  19.  
  20. paux = new struct point<T>;
  21. paux->x = x;
  22. paux->y = y;
  23. paux->prev = NULL;
  24. paux->next = pfirst;
  25.  
  26. if (pfirst != NULL) pfirst->prev = paux;
  27. pfirst = paux;
  28.  
  29. if (plast==NULL) plast=pfirst;
  30. }
  31.  
  32. void addLast(T x, T y) {
  33. struct point<T> *paux;
  34.  
  35. paux = new struct point<T>;
  36. paux->x = x;
  37. paux->y = y;
  38. paux->prev = plast;
  39. paux->next = NULL;
  40.  
  41. if (plast != NULL) plast->next = paux;
  42. plast = paux;
  43. if (pfirst == NULL) pfirst = plast;
  44. }
  45. void removeFirst() {
  46. struct point<T>* paux;
  47.  
  48. if (pfirst != NULL) {
  49. paux = pfirst->next;
  50. if (pfirst == plast) plast = NULL;
  51. delete pfirst;
  52. pfirst = paux;
  53. if (pfirst != NULL) pfirst->prev = NULL;
  54. }
  55. else cout<<"The list is empty"<<endl;
  56. }
  57.  
  58. void removeLast() {
  59. struct point<T> *paux;
  60.  
  61. if (plast != NULL) {
  62. paux = plast->prev;
  63. if (pfirst == plast) pfirst = NULL;
  64. delete plast;
  65. plast = paux;
  66. if (plast != NULL) plast->next = NULL;
  67. }
  68. else cout<<"The list is empty"<<endl;
  69. }
  70. struct point<T>* findFirstOccurrence(T x, T y) {
  71. struct point<T> *paux;
  72.  
  73. paux = pfirst;
  74. while (paux != NULL) {
  75. if (paux->x == x && paux->y == y)
  76. return paux;
  77. paux = paux->next;
  78. }
  79. return NULL;
  80. }
  81.  
  82. struct point<T>* findLastOccurrence(T x, T y) {
  83. struct point<T> *paux;
  84.  
  85. paux = plast;
  86. while (paux != NULL) {
  87. if (paux->x == x && paux->y == y)
  88. return paux;
  89. paux = paux->prev;
  90. }
  91.  
  92. return NULL;
  93. }
  94.  
  95. int isEmpty() {
  96. return (pfirst == NULL);
  97. }
  98.  
  99. LinkedList() {
  100. pfirst = plast = NULL;
  101. }
  102.  
  103. void printList()
  104. {
  105. struct point<T> *p;
  106.  
  107. p = pfirst;
  108. while (p != NULL)
  109. {
  110. printf("%d %d\n", p->x , p->y);
  111. p = p->next;
  112. }
  113. }
  114.  
  115.  
  116. struct point<T> *merge(struct point<T> *first, struct point<T> *second){
  117.  
  118. if (!first)
  119. return second;
  120.  
  121. if (!second)
  122. return first;
  123.  
  124. if(sqrt((first->x)^2+(first->y)^2) < sqrt((second->x)^2+(second->y)^2))
  125.  
  126. {
  127. first->next = merge(first->next,second);
  128. first->next->prev = first;
  129. first->prev = NULL;
  130. return first;
  131. }
  132. else
  133. {
  134. second->next = merge(first,second->next);
  135. second->next->prev = second;
  136. second->prev = NULL;
  137. return second;
  138. }
  139. }
  140.  
  141. struct point<T> *mergeSort(struct point<T> *head)
  142. {
  143. if (!head || !head->next)
  144. return head;
  145. struct point<T> *second = split(head);
  146.  
  147. // Recur for left and right halves
  148. head = mergeSort(head);
  149. second = mergeSort(second);
  150.  
  151. // Merge the two sorted halves
  152. return merge(head,second);
  153. }
  154.  
  155. void sort() {
  156. pfirst = mergeSort(pfirst);
  157. }
  158.  
  159. struct point<T> *split(struct point<T> *head)
  160. {
  161. struct point<T> *fast = head,*slow = head;
  162. while (fast->next && fast->next->next)
  163. {
  164. fast = fast->next->next;
  165. slow = slow->next;
  166. }
  167. struct point<T> *temp = slow->next;
  168. slow->next = NULL;
  169. return temp;
  170. }
  171.  
  172. };
  173.  
  174. int main()
  175. { int n,x,y;
  176. LinkedList<int> l;
  177. cout<<"Number of points: ";
  178. cin>>n;
  179.  
  180. for(int i=0; i<n;i++)
  181. {
  182. cout<<"For "<<i<<" point enter: "<<endl;
  183. cout<<"x= ";
  184. cin>>x;
  185. cout<<"y= ";
  186. cin>>y;
  187. l.addLast(x,y);
  188. }
  189. l.printList();
  190. l.sort();
  191. cout<< endl;
  192. cout << "Sorted list: "<< endl;
  193. l.printList();
  194.  
  195. return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement