#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[]) {
for (int i = 0; i < n; i++) {
Node *newNode = createN(a[i]); // 新しいノードを作成
if (head == NULL || head->val >= newNode->val) {
// 挿入位置が先頭の場合
newNode->next = head;
head = newNode;
} else {
// 挿入位置を探索
Node *p = head;
while (p->next != NULL && p->next->val < newNode->val) {
p = p->next;
}
// 現在のノード `p` の後に挿入
newNode->next = p->next;
p->next = newNode;
}
}
}
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+dmFsID0geDsKICAgIG5ld25vZGUtPm5leHQgPSBOVUxMOwogICAgcmV0dXJuIG5ld25vZGU7Cn0KCnZvaWQgZnJlZUwoKSB7CiAgICBOb2RlICpwOwogICAgd2hpbGUgKGhlYWQgIT0gTlVMTCkgewogICAgICAgIHAgPSBoZWFkLT5uZXh0OwogICAgICAgIGZyZWUoaGVhZCk7CiAgICAgICAgaGVhZCA9IHA7CiAgICB9Cn0KCnZvaWQgcHJpbnRMKCkgewogICAgTm9kZSAqcCA9IGhlYWQ7CiAgICB3aGlsZSAocCAhPSBOVUxMKSB7CiAgICAgICAgcHJpbnRmKCIlZCAiLCBwLT52YWwpOwogICAgICAgIHAgPSBwLT5uZXh0OwogICAgfQogICAgcHJpbnRmKCJcbiIpOwp9Cgp2b2lkIG1ha2VMKGludCBuLCBpbnQgYVtdKSB7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIE5vZGUgKm5ld05vZGUgPSBjcmVhdGVOKGFbaV0pOyAvLyDmlrDjgZfjgYTjg47jg7zjg4njgpLkvZzmiJAKICAgICAgICBpZiAoaGVhZCA9PSBOVUxMIHx8IGhlYWQtPnZhbCA+PSBuZXdOb2RlLT52YWwpIHsKICAgICAgICAgICAgLy8g5oy/5YWl5L2N572u44GM5YWI6aCt44Gu5aC05ZCICiAgICAgICAgICAgIG5ld05vZGUtPm5leHQgPSBoZWFkOwogICAgICAgICAgICBoZWFkID0gbmV3Tm9kZTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAvLyDmjL/lhaXkvY3nva7jgpLmjqLntKIKICAgICAgICAgICAgTm9kZSAqcCA9IGhlYWQ7CiAgICAgICAgICAgIHdoaWxlIChwLT5uZXh0ICE9IE5VTEwgJiYgcC0+bmV4dC0+dmFsIDwgbmV3Tm9kZS0+dmFsKSB7CiAgICAgICAgICAgICAgICBwID0gcC0+bmV4dDsKICAgICAgICAgICAgfQogICAgICAgICAgICAvLyDnj77lnKjjga7jg47jg7zjg4kgYHBgIOOBruW+jOOBq+aMv+WFpQogICAgICAgICAgICBuZXdOb2RlLT5uZXh0ID0gcC0+bmV4dDsKICAgICAgICAgICAgcC0+bmV4dCA9IG5ld05vZGU7CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbih2b2lkKSB7CiAgICBpbnQgaSwgbjsKICAgIGludCAqYTsKICAgIHNjYW5mKCIlZCIsICZuKTsKICAgIGEgPSAoaW50ICopbWFsbG9jKHNpemVvZihpbnQpICogbik7CiAgICBmb3IgKGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgc2NhbmYoIiVkIiwgJmFbaV0pOwogICAgfQogICAgbWFrZUwobiwgYSk7CiAgICBwcmludEwoKTsKICAgIGZyZWVMKCk7CiAgICBmcmVlKGEpOwogICAgcmV0dXJuIDA7Cn0=