SHARE
TWEET

Untitled

a guest Feb 24th, 2020 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void swap(Node** head, int index1, int index2)
  2. {
  3.     if (*head == nullptr)
  4.         return;
  5.  
  6.     Node *a = nullptr, *b = nullptr, *t = *head;
  7.  
  8.     for (int i = 0; i <= std::max(index1, index2); ++i)
  9.     {
  10.         if (i == index1)
  11.             a = t;
  12.  
  13.         if (i == index2)
  14.             b = t;
  15.    
  16.         t = t->next;
  17.     }
  18.  
  19.     Node *a_prev = a->prev, *a_next = a->next,
  20.          *b_prev = b->prev, *b_next = b->next;
  21.  
  22.     if (a_next != nullptr)
  23.         a_next->prev = b;
  24.     if (a_prev != nullptr)
  25.         a_prev->next = b;
  26.  
  27.     if (b_next != nullptr)
  28.         b_next->prev = a;
  29.     if (b_prev != nullptr)
  30.         b_prev->next = a;
  31.  
  32.     a->next = b_next;
  33.     a->prev = b_prev;
  34.  
  35.     b->next = a_next;
  36.     b->prev = a_prev;
  37.  
  38.     if (index1 == 0)
  39.         *head = b;
  40.  
  41.     if (index2 == 0)
  42.         *head = a;
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top