Skip to content

Instantly share code, notes, and snippets.

@bluesunxu
Created December 17, 2012 22:01
Show Gist options
  • Save bluesunxu/4322723 to your computer and use it in GitHub Desktop.
Save bluesunxu/4322723 to your computer and use it in GitHub Desktop.
Insertion into a cyclic sorted list with known head pointer of the smallest element
node* icsl_1(node* head, node* n) {
if(!head) {
// list is empty, set head to the new node.
return (head = n->next = n);
}
node* cur=head;
node* prev;
// the new element is less than the smallest in the list
if(n->key < head->key) {
while(cur->next != head)
cur=cur->next;
n->next = head;
return (head = cur->next = n);
}
// normal situation or new element is larger than the biggest
do {
prev = cur;
cur = cur->next;
if(n->value <= cur->value) break;
} while (cur != head);
n->next = cur;
prev->next = n;
return head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment