Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. /*
  2. prawie posortowana tablica, co najmniej 2 elementy, jeden losowy podmieniony losowo
  3. trzeba poprawić
  4. */
  5.  
  6. struct Node {
  7. Node *next;
  8. int value;
  9. };
  10.  
  11. bool isUp(node* first) {
  12. if (first == nullptr)
  13. return true;
  14. int up=0, down=0;
  15. for (int i=0; i<4 && first->next != nullptr; i++, first = first->next)
  16. if (first->val < first->next->val)
  17. up++;
  18. else
  19. down++;
  20. return up > down;
  21. }
  22.  
  23. void fixSortedList(Node *&L) {
  24. if (L == nullptr || L->next == nullptr || L->next->next == nullptr)
  25. return;
  26.  
  27. bool up = isUp(L);
  28.  
  29. node* guard = new Node;
  30. guard->next = L;
  31.  
  32. Node* diff = nullptr
  33. if ((up && L->value > L->next->value) || (!up && L->value < L->next->value)) {
  34. diff = L;
  35. guard->next = L->next;
  36. } else {
  37. while ((up && L->value < L->next->value) || (!up && L->value > L->next->value))
  38. L = L->next;
  39. diff = L->next;
  40. L->next = diff->next;
  41. }
  42. if (diff != nullptr) {
  43. L = guard;
  44. while ((up && L->next->value < diff->value ) || (!up && L->next->value > diff->value))
  45. L = L->next;
  46. diff->next = L->next;
  47. L->next = diff->next;
  48. }
  49. L = guard->next;
  50. delete guard;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement