Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- prawie posortowana tablica, co najmniej 2 elementy, jeden losowy podmieniony losowo
- trzeba poprawić
- */
- struct Node {
- Node *next;
- int value;
- };
- bool isUp(node* first) {
- if (first == nullptr)
- return true;
- int up=0, down=0;
- for (int i=0; i<4 && first->next != nullptr; i++, first = first->next)
- if (first->val < first->next->val)
- up++;
- else
- down++;
- return up > down;
- }
- void fixSortedList(Node *&L) {
- if (L == nullptr || L->next == nullptr || L->next->next == nullptr)
- return;
- bool up = isUp(L);
- node* guard = new Node;
- guard->next = L;
- Node* diff = nullptr
- if ((up && L->value > L->next->value) || (!up && L->value < L->next->value)) {
- diff = L;
- guard->next = L->next;
- } else {
- while ((up && L->value < L->next->value) || (!up && L->value > L->next->value))
- L = L->next;
- diff = L->next;
- L->next = diff->next;
- }
- if (diff != nullptr) {
- L = guard;
- while ((up && L->next->value < diff->value ) || (!up && L->next->value > diff->value))
- L = L->next;
- diff->next = L->next;
- L->next = diff->next;
- }
- L = guard->next;
- delete guard;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement