Created
December 17, 2012 22:01
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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