Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void linkedList::sort()
- {
- //start iterating at head
- Node* current = head;
- //iterate until last node in list
- while (current->next != null) {
- //set smallest as current
- Node* smallest = current;
- //iterator starts at the next
- Node* iterator = current->next;
- //iterate through rest of list finding smallest node
- while (iterator->next != null) {
- if (iterator->data < smallest->data)
- smallest = iterator;
- iterator = iterator->next;
- }
- //if current isn't smallest swith em
- if (current != smallest) {
- //current->prev is null then its head, swith with smallest
- if (current->prev == NULL) {
- head = smallest;
- }
- //get temp vars to use for current from smallest
- Node *temp_next = smallest->next;
- Node *temp_prev = smallest->prev;
- Node *temp = smallest;
- //set smallest links to currents;
- smallest = current;
- smallest->next = current->next;
- if (smallest->next != NULL)
- smallest->next->prev = smallest;
- smallest->prev = current->prev;
- if (smallest->prev != NULL)
- smallest->prev->next = smallest;
- //set currents links to saved smallests
- current = temp;
- current->next = temp_next;
- if (current->next != NULL)
- current->next->prev = current;
- current->prev = temp_prev;
- if (current->prev != NULL)
- current->prev->next = current;
- }
- //go to next in list
- current = smallest->next;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement