Advertisement
Guest User

Untitled

a guest
May 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct Node {
  7. int value;
  8. Node* next;
  9. Node* prev;
  10.  
  11. Node(int v) {
  12. value = v;
  13. }
  14.  
  15. Node() {}
  16. };
  17.  
  18. struct List {
  19. Node* head = NULL;
  20. Node* last = NULL;
  21. int size = 0;
  22.  
  23. List() {};
  24.  
  25. List(Node* first, Node* last, int size) {
  26.  
  27. this->head = first;
  28. this->last = last;
  29. this->size = size;
  30.  
  31. }
  32.  
  33. List* add(int v) {
  34.  
  35. if (head == NULL) {
  36. head = new Node(v);
  37. size++;
  38. last = head;
  39. last->next = NULL;
  40. last->prev = NULL;
  41. }
  42.  
  43. else {
  44. last->next = new Node(v);
  45. last->next->prev = last;
  46. size++;
  47. last = last->next;
  48. last->next = NULL;
  49. }
  50.  
  51. return this;
  52. }
  53.  
  54.  
  55. void unshitf(int v) {
  56. if (size == 0) {
  57. head = new Node(v);
  58. size++;
  59. last = head;
  60. last->next = NULL;
  61. last->prev = NULL;
  62. }
  63. else {
  64. head->prev = new Node(v);
  65. head->prev->next = head;
  66. head = head->prev;
  67. size++;
  68.  
  69.  
  70. }
  71. }
  72.  
  73. void wypisz() {
  74. Node* tmp = head;
  75. for (int i = 0; i < size; i++) {
  76. cout << tmp->value << " ";
  77. cout << "Poprzedni: " << tmp->prev << " Nastepny: " << tmp->next << endl;
  78. tmp = tmp->next;
  79.  
  80. }
  81. cout << endl;
  82. }
  83.  
  84. void readFromFile(string filename) {
  85.  
  86. ifstream input(filename);
  87. while (!input.eof()) {
  88. int tmp;
  89. input >> tmp;
  90. this->add(tmp);
  91. }
  92. input.close();
  93. }
  94.  
  95. void deleteValue(int v) {
  96.  
  97. Node* tmp = head;
  98. if (head->value == v) {
  99. tmp = head->next;
  100. delete head;
  101. head = tmp;
  102. tmp->prev = NULL;
  103. size--;
  104. }
  105.  
  106. do {
  107. if (tmp->value == v) {
  108. tmp->prev->next = tmp->next;
  109. tmp->next->prev = tmp->prev;
  110. Node* tmp2 = tmp->next;
  111. delete tmp;
  112. size--;
  113. tmp = tmp2;
  114. }
  115.  
  116. else tmp = tmp->next;
  117. } while (tmp->next != NULL);
  118. if (tmp->value == v) {
  119. tmp->prev->next = NULL;
  120. delete tmp;
  121. size--;
  122. }
  123.  
  124. }
  125.  
  126. void addAt(int index, int value) {
  127.  
  128. if (index <= 0) { unshitf(value); return; }
  129.  
  130.  
  131. Node* tmp = head;
  132. for (int i = 0; i < size; i++) {
  133.  
  134. if (i == index) {
  135. tmp->prev->next = new Node(value);
  136. tmp->prev->next->next = tmp;
  137. tmp->prev->next->prev = tmp->prev;
  138. tmp->prev = tmp->prev->next;
  139. size++;
  140. return;
  141. }
  142. tmp = tmp->next;
  143. }
  144.  
  145. add(value);
  146.  
  147. }
  148.  
  149.  
  150. void swap(int index1, int index2) {
  151.  
  152. Node* tmp = head;
  153. Node* tmp1 = NULL;
  154. Node* tmp1Prev = NULL;
  155. Node* tmp1Next = NULL;
  156. Node* tmp2 = NULL;
  157. Node* tmp2Prev = NULL;
  158. Node* tmp2Next = NULL;
  159.  
  160. if (index1 == index2)return;
  161.  
  162. if (index1 > index2) {
  163. int tmp = index1;
  164. index1 = index2;
  165. index2 = tmp;
  166.  
  167. }
  168.  
  169.  
  170.  
  171. for (int i = 0; i < size; i++) {
  172. if (i == index1) {
  173. tmp1 = tmp;
  174. tmp1Next = tmp1->next;
  175. tmp1Prev = tmp1->prev;
  176.  
  177. }
  178. if (i == index2) {
  179. tmp2 = tmp;
  180. tmp2Next = tmp2->next;
  181. tmp2Prev = tmp2->prev;
  182.  
  183. }
  184.  
  185. tmp = tmp->next;
  186.  
  187. }
  188.  
  189. if (index2 == size - 1 && index1 == 0) {
  190.  
  191. tmp1->next = NULL;
  192. tmp1->prev = tmp2Prev;
  193. tmp1->prev->next = tmp1;
  194.  
  195. tmp2->prev = NULL;
  196. tmp2->next = tmp1Next;
  197. tmp2->next->prev = tmp2;
  198.  
  199. head = tmp2;
  200. last = tmp1;
  201.  
  202.  
  203. return;
  204. }
  205.  
  206. if (index2 == size - 1 && index1 == size - 2) {
  207. last = tmp1;
  208. tmp1->next = NULL;
  209. tmp1->prev = tmp2;
  210. tmp1->prev->next = tmp1;
  211.  
  212.  
  213.  
  214. tmp2->prev = tmp1Prev;
  215. tmp2->prev->next = tmp2;
  216.  
  217.  
  218. return;
  219. }
  220.  
  221.  
  222. if (index2 == size - 1) {
  223. last = tmp1;
  224. tmp1->next = NULL;
  225. tmp1->prev = tmp2Prev;
  226. tmp1->prev->next = tmp1;
  227.  
  228.  
  229. tmp2->next = tmp1Next;
  230. tmp2->prev = tmp1Prev;
  231. tmp2->prev->next = tmp2;
  232. tmp2->next->prev = tmp2;
  233.  
  234. return;
  235. }
  236.  
  237.  
  238. if (index2 - index1 == 1) {
  239. tmp1->next = tmp2->next;
  240. tmp1->prev = tmp2;
  241. tmp2->next = tmp1;
  242. tmp2->prev = tmp1Prev;
  243. if (tmp1Next != NULL)tmp2Next->prev = tmp1;
  244. if (index1 == 0) {
  245. head = tmp2;
  246. tmp2->prev = NULL;
  247.  
  248. }
  249.  
  250. else { tmp1Prev->next = tmp2; }
  251.  
  252.  
  253.  
  254.  
  255. return;
  256. }
  257.  
  258. if (index1 == 0) {
  259.  
  260. tmp2->prev = NULL;
  261. tmp2->next = tmp1Next;
  262. tmp2->next->prev = tmp2;
  263.  
  264.  
  265. tmp2Prev->next = tmp1;
  266. tmp2Next->prev = tmp1;
  267. tmp1->next = tmp2Next;
  268. tmp1->prev = tmp2Prev;
  269. head = tmp2;
  270.  
  271. return;
  272. }
  273.  
  274.  
  275. if (tmp1 == NULL || tmp2 == NULL) return;
  276.  
  277. //Zamiana
  278. tmp1Prev->next = tmp2;
  279. tmp1Next->prev = tmp2;
  280. tmp2->next = tmp1Next;
  281. tmp2->prev = tmp1Prev;
  282.  
  283.  
  284. tmp2Prev->next = tmp1;
  285. tmp2Next->prev = tmp1;
  286. tmp1->next = tmp2Next;
  287. tmp1->prev = tmp2Prev;
  288. }
  289.  
  290.  
  291. int getIndex(Node* node) {
  292.  
  293. Node* tmp = head;
  294. for (int i = 0; i < size; i++) {
  295. if (node == tmp) return i;
  296.  
  297. tmp = tmp->next;
  298. }
  299.  
  300. return -1;
  301. }
  302.  
  303.  
  304.  
  305.  
  306. void insertSort()
  307. {
  308. int i, j;
  309.  
  310. i = 1;
  311. while (i < size) {
  312. j = i + 1;
  313.  
  314. while (j > 0 && get(j - 1) > get(j))
  315. {
  316. swap(j, j - 1);
  317. j = j - 1;
  318. }
  319. i = i + 1;
  320. }
  321. }
  322.  
  323. void set(int index, int value) {
  324. Node* tmp = head;
  325.  
  326. for (int i = 0; i < size; i++) {
  327.  
  328. if (i == index) {
  329. tmp->value = value;
  330. return;
  331. }
  332. tmp = tmp->next;
  333. }
  334.  
  335.  
  336. }
  337.  
  338. int get(int index) {
  339. Node* tmp = head;
  340.  
  341. for (int i = 0; i < size; i++) {
  342.  
  343. if (i == index) {
  344. return tmp->value;
  345. }
  346. tmp = tmp->next;
  347. }
  348.  
  349.  
  350. }
  351.  
  352. List* crop(int index) {
  353. if (index >= this->size) {
  354. return NULL;
  355. }
  356.  
  357. Node* tmp = head;
  358. List* newList;
  359.  
  360. for (int i = 0; i < size; i++) {
  361.  
  362. if (i == index) {
  363. newList = new List(tmp, this->last, this->size - i);
  364. this->last = tmp->prev;
  365. this->size = i;
  366. return newList;
  367. }
  368. tmp = tmp->next;
  369. }
  370.  
  371. }
  372.  
  373.  
  374. void sort() {
  375. Node* tmp = head;
  376.  
  377. for (int i = 0; i < size; i++) {
  378. for (int j = 0; j < size - 1; j++) {
  379. if (tmp == NULL || tmp->next == NULL) break;
  380. if (tmp->value < tmp->next->value) {
  381. this->swap(j, j + 1);
  382.  
  383. }
  384.  
  385. tmp = tmp->next;
  386. }
  387. tmp = head;
  388.  
  389. }
  390. }
  391.  
  392.  
  393.  
  394. int maxValueIndex(int from = 0) {
  395.  
  396. Node* tmp = head;
  397. int max = 0;
  398. int maxIndex = 0;
  399.  
  400.  
  401. for (int i = 0; i < size; i++) {
  402. if (tmp->value > max && i >= from) {
  403. max = tmp->value;
  404. maxIndex = i;
  405.  
  406. }
  407.  
  408. tmp = tmp->next;
  409. }
  410. return maxIndex;
  411. }
  412.  
  413.  
  414.  
  415.  
  416. void selectSort() {
  417.  
  418.  
  419. for (int i = 0; i < size - 1; i++) {
  420. int index = maxValueIndex(i);
  421.  
  422. swap(i, index);
  423.  
  424. }
  425.  
  426.  
  427. }
  428.  
  429. Node* merge(Node *first, Node *second)
  430. {
  431. //Jesli pierwszy jest pusty
  432. if (!first)
  433. return second;
  434.  
  435. //Jesli drugi jest pusty
  436. if (!second)
  437. return first;
  438.  
  439. // Wybierz mniejsza watrosc
  440. if (first->value < second->value)
  441. {
  442. first->next = merge(first->next, second);
  443. first->next->prev = first;
  444. first->prev = NULL;
  445. return first;
  446. }
  447. else
  448. {
  449. second->next = merge(first, second->next);
  450. second->next->prev = second;
  451. second->prev = NULL;
  452. return second;
  453. }
  454. }
  455.  
  456.  
  457. Node* mergeSort()
  458. {
  459. return mergeSort(this->head);
  460. }
  461.  
  462.  
  463. Node* mergeSort(Node*& head)
  464. {
  465. if (!head || !head->next)
  466. return head;
  467. Node* second = split(head);
  468.  
  469.  
  470. head = mergeSort(head);
  471. second = mergeSort(second);
  472.  
  473.  
  474. return merge(head, second);
  475. }
  476.  
  477.  
  478.  
  479. Node* split(Node* head)
  480. {
  481. Node* list1 = head, *list2 = head;
  482. while (list1->next && list1->next->next)
  483. {
  484. list1 = list1->next->next;
  485. list2 = list2->next;
  486. }
  487. Node* tmp = list2->next;
  488. list2->next = NULL;
  489. return tmp;
  490. }
  491.  
  492. void insertSort(Node *head) {
  493.  
  494. Node* tmp = head;
  495. Node* obecny = head->next;
  496. Node* prev = head;
  497.  
  498. //jesli jest sa mniej niz 2 elementy to nie ma co sortowac
  499. if (!tmp || !tmp->next) {
  500. return;
  501. }
  502.  
  503. while (obecny) {
  504.  
  505. //pomin posortowane elementy
  506.  
  507. if (prev->value <= obecny->value) {
  508. obecny = obecny->next;
  509. prev = prev->next;
  510. }
  511.  
  512. else {
  513. //wstaw przed glowe
  514. if (head->value > obecny->value) {
  515.  
  516. prev->next = obecny->next;
  517. obecny->next = head;
  518. head = obecny;
  519.  
  520. }
  521. else {
  522.  
  523. //wstaw w odpowiednie miejsce
  524. tmp = head;
  525.  
  526. while (!tmp->next && tmp->next->value < obecny->value) {
  527. tmp = tmp->next;
  528. }
  529.  
  530. prev->next = obecny->next;
  531. obecny->next = tmp->next;
  532. tmp->next = obecny;
  533. tmp->next->prev = tmp;
  534.  
  535. }
  536. }
  537.  
  538. obecny->next->prev = obecny;
  539. obecny = prev->next;
  540.  
  541. }
  542. }
  543.  
  544.  
  545.  
  546. void addBeforeFirst(Node *& head, double value) {
  547.  
  548. Node * newNode = new Node;
  549. newNode->value = value;
  550.  
  551. if (head == NULL) {
  552. head = newNode;
  553. newNode->next = NULL;
  554.  
  555. }
  556. else {
  557. newNode->next = head;
  558. head = newNode;
  559.  
  560. newNode->prev = NULL;
  561. newNode->next->prev = head;
  562. }
  563.  
  564. }
  565.  
  566. int partitionLomuto(Node * head, int l, int r) {
  567.  
  568. //split using far right element
  569. double x = get(r);
  570.  
  571. //DEBUG
  572. std::cout << "Value: " << x << " Index l: " << l << " Index r: " << r << "\n";
  573. //DEBUG
  574.  
  575. int i = l - 1;
  576.  
  577. for (int j = l; j < r; j++)
  578.  
  579. if (get( j) < x) {
  580. i++;
  581. swap(i, j);
  582. }
  583.  
  584. swap(i + 1, r);
  585.  
  586. return i + 1;
  587. }
  588.  
  589. void quickSort(Node * head, int l, int r) {
  590.  
  591. if (l >= r) {
  592. return;
  593. }
  594.  
  595. int pivot = partitionLomuto(head, l, r);
  596.  
  597. quickSort(head, l, pivot - 1);
  598. quickSort(head, pivot + 1, r);
  599. }
  600. void quickSortV2(Node *& head) {
  601. this->add(NULL);
  602. //add sentry
  603. addBeforeFirst(head, 0);
  604.  
  605. //get length of node
  606. Node * tmp = head;
  607. int i = 0;
  608. while (tmp) {
  609. i++;
  610. tmp = tmp->next;
  611. }
  612.  
  613. //quickSort
  614. quickSort(head, 1, i - 1);
  615.  
  616. //remove sentry
  617. // Node * backupHead = head;
  618. //head = head->next;
  619. // delete backupHead;
  620.  
  621. //set prev pointer in first element
  622. head->prev = NULL;
  623. deleteValue(NULL);
  624. }
  625.  
  626.  
  627. };
  628.  
  629.  
  630.  
  631. int main()
  632. {
  633. List* list = new List();
  634.  
  635. //list->readFromFile("test.txt");
  636.  
  637. list->add(1)->add(4)->add(3)->add(2)->add(8)->add(1)->add(15)->add(10)->add(11)->add(7)->add(9);
  638. list->wypisz();
  639.  
  640. //list->insertSort();
  641.  
  642.  
  643. //list->mergeSort();
  644. list->quickSortV2(list->head);
  645. list->wypisz();
  646.  
  647.  
  648.  
  649. cout << endl << "Liczba elementow:" << list->size << endl;
  650.  
  651.  
  652. system("pause");
  653. return 0;
  654. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement