Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
IDL 1.40 KB | None | 0 0
  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.         //if current isn't smallest swith em
  18.         if (current != smallest) {
  19.             //current->prev is null then its head, swith with smallest
  20.             if (current->prev == NULL) {
  21.                 head = smallest;
  22.             }
  23.             //get temp vars to use for current from smallest
  24.             Node *temp_next = smallest->next;
  25.             Node *temp_prev = smallest->prev;
  26.             Node *temp = smallest;
  27.  
  28.             //set smallest links to currents;
  29.             smallest = current;
  30.             smallest->next = current->next;
  31.             if (smallest->next != NULL)
  32.                 smallest->next->prev = smallest;
  33.             smallest->prev = current->prev;
  34.             if (smallest->prev != NULL)
  35.                 smallest->prev->next = smallest;
  36.                
  37.             //set currents links to saved smallests
  38.             current = temp;
  39.             current->next = temp_next;
  40.             if (current->next != NULL)
  41.                 current->next->prev = current;
  42.             current->prev = temp_prev;
  43.             if (current->prev != NULL)
  44.                 current->prev->next = current;
  45.         }  
  46.                
  47.         //go to next in list
  48.         current = smallest->next;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement