#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node *next;
} Node;
Node *head = NULL;
Node* createN(int x) {
Node
*newnode
= (Node
*)malloc(sizeof(Node
)); newnode->val = x;
newnode->next = NULL;
return newnode;
}
void freeL() {
Node *p;
while (head != NULL) {
p = head->next;
head = p;
}
}
void printL() {
Node *p = head;
while (p != NULL) {
p = p->next;
}
}
void makeL(int n, int a[]) {
head = NULL; // リストを初期化
for (int i = 0; i < n; i++) {
Node *new_node = createN(a[i]); // 新しいノードを作成
if (head == NULL || head->val >= new_node->val) {
// 先頭に挿入
new_node->next = head;
head = new_node;
} else {
// 適切な位置を探して挿入
Node *p = head;
while (p->next != NULL && p->next->val < new_node->val) {
p = p->next;
}
new_node->next = p->next;
p->next = new_node;
}
}
}
int main(void) {
int i, n;
int *a;
a
= (int *)malloc(sizeof(int) * n
); for (i = 0; i < n; i++) {
}
makeL(n, a);
printL();
freeL();
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnR5cGVkZWYgc3RydWN0IG5vZGUgewogICAgaW50IHZhbDsKICAgIHN0cnVjdCBub2RlICpuZXh0Owp9IE5vZGU7CgpOb2RlICpoZWFkID0gTlVMTDsKCk5vZGUqIGNyZWF0ZU4oaW50IHgpIHsKICAgIE5vZGUgKm5ld25vZGUgPSAoTm9kZSAqKW1hbGxvYyhzaXplb2YoTm9kZSkpOwogICAgbmV3bm9kZS0+dmFsID0geDsKICAgIG5ld25vZGUtPm5leHQgPSBOVUxMOwogICAgcmV0dXJuIG5ld25vZGU7Cn0KCnZvaWQgZnJlZUwoKSB7CiAgICBOb2RlICpwOwogICAgd2hpbGUgKGhlYWQgIT0gTlVMTCkgewogICAgICAgIHAgPSBoZWFkLT5uZXh0OwogICAgICAgIGZyZWUoaGVhZCk7CiAgICAgICAgaGVhZCA9IHA7CiAgICB9Cn0KCnZvaWQgcHJpbnRMKCkgewogICAgTm9kZSAqcCA9IGhlYWQ7CiAgICB3aGlsZSAocCAhPSBOVUxMKSB7CiAgICAgICAgcHJpbnRmKCIlZCAiLCBwLT52YWwpOwogICAgICAgIHAgPSBwLT5uZXh0OwogICAgfQogICAgcHJpbnRmKCJcbiIpOwp9Cgp2b2lkIG1ha2VMKGludCBuLCBpbnQgYVtdKSB7CiAgICBoZWFkID0gTlVMTDsgLy8g44Oq44K544OI44KS5Yid5pyf5YyWCgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBOb2RlICpuZXdfbm9kZSA9IGNyZWF0ZU4oYVtpXSk7IC8vIOaWsOOBl+OBhOODjuODvOODieOCkuS9nOaIkAogICAgICAgIGlmIChoZWFkID09IE5VTEwgfHwgaGVhZC0+dmFsID49IG5ld19ub2RlLT52YWwpIHsKICAgICAgICAgICAgLy8g5YWI6aCt44Gr5oy/5YWlCiAgICAgICAgICAgIG5ld19ub2RlLT5uZXh0ID0gaGVhZDsKICAgICAgICAgICAgaGVhZCA9IG5ld19ub2RlOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIC8vIOmBqeWIh+OBquS9jee9ruOCkuaOouOBl+OBpuaMv+WFpQogICAgICAgICAgICBOb2RlICpwID0gaGVhZDsKICAgICAgICAgICAgd2hpbGUgKHAtPm5leHQgIT0gTlVMTCAmJiBwLT5uZXh0LT52YWwgPCBuZXdfbm9kZS0+dmFsKSB7CiAgICAgICAgICAgICAgICBwID0gcC0+bmV4dDsKICAgICAgICAgICAgfQogICAgICAgICAgICBuZXdfbm9kZS0+bmV4dCA9IHAtPm5leHQ7CiAgICAgICAgICAgIHAtPm5leHQgPSBuZXdfbm9kZTsKICAgICAgICB9CiAgICB9Cn0KCmludCBtYWluKHZvaWQpIHsKICAgIGludCBpLCBuOwogICAgaW50ICphOwogICAgc2NhbmYoIiVkIiwgJm4pOwogICAgYSA9IChpbnQgKiltYWxsb2Moc2l6ZW9mKGludCkgKiBuKTsKICAgIGZvciAoaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBzY2FuZigiJWQiLCAmYVtpXSk7CiAgICB9CiAgICBtYWtlTChuLCBhKTsKICAgIHByaW50TCgpOwogICAgZnJlZUwoKTsKICAgIGZyZWUoYSk7CiAgICByZXR1cm4gMDsKfQo=