Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void swap(Node** head, int index1, int index2)
- {
- if (*head == nullptr)
- return;
- Node *a = nullptr, *b = nullptr, *t = *head;
- for (int i = 0; i <= std::max(index1, index2); ++i)
- {
- if (i == index1)
- a = t;
- if (i == index2)
- b = t;
- t = t->next;
- }
- Node *a_prev = a->prev, *a_next = a->next,
- *b_prev = b->prev, *b_next = b->next;
- if (a_next != nullptr)
- a_next->prev = b;
- if (a_prev != nullptr)
- a_prev->next = b;
- if (b_next != nullptr)
- b_next->prev = a;
- if (b_prev != nullptr)
- b_prev->next = a;
- a->next = b_next;
- a->prev = b_prev;
- b->next = a_next;
- b->prev = a_prev;
- if (index1 == 0)
- *head = b;
- if (index2 == 0)
- *head = a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement