SHARE
TWEET

Untitled

a guest Jun 27th, 2017 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void linkedList::sort()
  2. {
  3.     //start iterating at head
  4.     Node* current = head;
  5.     //iterate until last node in list
  6.     while (current->next != null) {
  7.         //set smallest as current
  8.         Node* smallest = current;
  9.         //iterator starts at the next
  10.         Node* iterator = current->next;
  11.         //iterate through rest of list finding smallest node
  12.         while (iterator->next != null) {
  13.             if (iterator->data < smallest->data)
  14.                 smallest = iterator;
  15.             iterator = iterator->next;
  16.         }
  17.  
  18.     //debuggan
  19. std::cout << smallest->next <<std::endl;
  20.         //if current isn't smallest swith em
  21.         if (current != smallest) {
  22.             //current->prev is null then its head, swith with smallest
  23.             if (current->prev == NULL) {
  24.                 head = smallest;
  25.             }
  26.             //get temp vars to use for current from smallest
  27.             Node *temp_next = smallest->next;
  28.             Node *temp_prev = smallest->prev;
  29.             Node *temp = smallest;
  30.  
  31.             //set smallest links to currents;
  32.             smallest = current;
  33.             smallest->next = current->next;
  34.             if (smallest->next != NULL)
  35.                 smallest->next->prev = smallest;
  36.             smallest->prev = current->prev;
  37.             if (smallest->prev != NULL)
  38.                 smallest->prev->next = smallest;
  39.                
  40.             //set currents links to saved smallests
  41.             current = temp;
  42.             current->next = temp_next;
  43.             if (current->next != NULL)
  44.                 current->next->prev = current;
  45.             current->prev = temp_prev;
  46.             if (current->prev != NULL)
  47.                 current->prev->next = current;
  48.         }  
  49.                
  50.         //go to next in list
  51.         current = smallest->next;
  52.     }
  53. }
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