#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node * ptr;
} node;
node* insert( node* head, int num) {
node * temp, * prev, * next;
temp
= ( node
* ) malloc ( sizeof ( node
) ) ; temp-> data = num;
temp-> ptr = NULL;
if ( ! head) {
head= temp;
} else {
prev = NULL;
next = head;
while ( next && next-> data<= num) {
prev = next;
next = next-> ptr;
}
if ( ! next) {
prev-> ptr = temp;
} else {
if ( prev) {
temp-> ptr = prev-> ptr;
prev-> ptr = temp;
} else {
temp-> ptr = head;
head = temp;
}
}
}
return head;
}
void free_list( node * head) {
node * prev = head;
node * cur = head;
while ( cur) {
prev = cur;
cur = prev-> ptr;
}
}
int main( ) {
int num;
node * head, * p;
head = NULL;
do {
if ( num) {
head = insert( head, num) ;
}
} while ( num) ;
p = head;
printf ( "\n The numbers are:\n " ) ; while ( p) {
p = p-> ptr;
}
free_list( head) ;
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnR5cGVkZWYgc3RydWN0IG5vZGV7CiAgICBpbnQgZGF0YTsKICAgIHN0cnVjdCBub2RlICpwdHI7Cn0gbm9kZTsKCm5vZGUqIGluc2VydChub2RlKiBoZWFkLCBpbnQgbnVtKSB7CiAgICBub2RlICp0ZW1wLCAqcHJldiwgKm5leHQ7CiAgICB0ZW1wID0gKG5vZGUqKW1hbGxvYyhzaXplb2Yobm9kZSkpOwogICAgdGVtcC0+ZGF0YSA9IG51bTsKICAgIHRlbXAtPnB0ciA9IE5VTEw7CiAgICBpZighaGVhZCl7CiAgICAgICAgaGVhZD10ZW1wOwogICAgfSBlbHNlewogICAgICAgIHByZXYgPSBOVUxMOwogICAgICAgIG5leHQgPSBoZWFkOwogICAgICAgIHdoaWxlKG5leHQgJiYgbmV4dC0+ZGF0YTw9bnVtKXsKICAgICAgICAgICAgcHJldiA9IG5leHQ7CiAgICAgICAgICAgIG5leHQgPSBuZXh0LT5wdHI7CiAgICAgICAgfQogICAgICAgIGlmKCFuZXh0KXsKICAgICAgICAgICAgcHJldi0+cHRyID0gdGVtcDsKICAgICAgICB9IGVsc2V7CiAgICAgICAgICAgIGlmKHByZXYpIHsKICAgICAgICAgICAgICAgIHRlbXAtPnB0ciA9IHByZXYtPnB0cjsKICAgICAgICAgICAgICAgIHByZXYtPiBwdHIgPSB0ZW1wOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgdGVtcC0+cHRyID0gaGVhZDsKICAgICAgICAgICAgICAgIGhlYWQgPSB0ZW1wOwogICAgICAgICAgICB9ICAgICAgICAgICAgCiAgICAgICAgfSAgIAogICAgfQogICAgcmV0dXJuIGhlYWQ7Cn0KCnZvaWQgZnJlZV9saXN0KG5vZGUgKmhlYWQpIHsKICAgIG5vZGUgKnByZXYgPSBoZWFkOwogICAgbm9kZSAqY3VyID0gaGVhZDsKICAgIHdoaWxlKGN1cikgewogICAgICAgIHByZXYgPSBjdXI7CiAgICAgICAgY3VyID0gcHJldi0+cHRyOwogICAgICAgIGZyZWUocHJldik7CiAgICB9ICAgICAgIAp9CgppbnQgbWFpbigpewogICAgaW50IG51bTsKICAgIG5vZGUgKmhlYWQsICpwOwogICAgaGVhZCA9IE5VTEw7CiAgICBkbyB7CiAgICAgICAgcHJpbnRmKCJFbnRlciBhIG51bWJlciIpOwogICAgICAgIHNjYW5mKCIlZCIsJm51bSk7CiAgICAgICAgaWYobnVtKSB7CiAgICAgICAgICAgIGhlYWQgPSBpbnNlcnQoaGVhZCwgbnVtKTsKICAgICAgICB9CiAgICB9IHdoaWxlKG51bSk7CiAgICBwID0gaGVhZDsKICAgIHByaW50ZigiXG5UaGUgbnVtYmVycyBhcmU6XG4iKTsKICAgIHdoaWxlKHApIHsKICAgICAgICBwcmludGYoIiVkICIsIHAtPmRhdGEpOwogICAgICAgIHAgPSBwLT5wdHI7CiAgICB9CiAgICBmcmVlX2xpc3QoaGVhZCk7CiAgICByZXR1cm4gMDsKfQ==