#include <stdio.h>
#include <stdlib.h>
struct list {
int value;
struct list *next;
};
void list_add(struct list **li, int value);
struct list *list_fromarray(int *v, int n);
void list_print(struct list *li, const char *msg);
void list_free(struct list *li);
int main(void) {
struct list *list;
int test[] = {1, 4, 8, 9, 13, 42};
list = list_fromarray(test, sizeof test/sizeof *test);
list_print(list, "final list");
list_free(list);
}
struct list *list_fromarray(int *v, int n) {
struct list *li = NULL;
for (int k = n - 1; k >= 0; k--) {
list_add(&li, v[k]);
}
return li;
}
void list_add(struct list **li, int value) {
struct list *node;
node->value = value;
node->next = *li;
*li = node;
}
void list_print(struct list *li, const char *msg) {
while (li) {
li = li->next;
}
}
void list_free(struct list *li) {
while (li) {
struct list *bak = li;
li = li->next;
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnN0cnVjdCBsaXN0IHsKICAgIGludCB2YWx1ZTsKICAgIHN0cnVjdCBsaXN0ICpuZXh0Owp9OwoKdm9pZCBsaXN0X2FkZChzdHJ1Y3QgbGlzdCAqKmxpLCBpbnQgdmFsdWUpOwpzdHJ1Y3QgbGlzdCAqbGlzdF9mcm9tYXJyYXkoaW50ICp2LCBpbnQgbik7CnZvaWQgbGlzdF9wcmludChzdHJ1Y3QgbGlzdCAqbGksIGNvbnN0IGNoYXIgKm1zZyk7CnZvaWQgbGlzdF9mcmVlKHN0cnVjdCBsaXN0ICpsaSk7CgppbnQgbWFpbih2b2lkKSB7CiAgICBzdHJ1Y3QgbGlzdCAqbGlzdDsKICAgIGludCB0ZXN0W10gPSB7MSwgNCwgOCwgOSwgMTMsIDQyfTsKICAgIGxpc3QgPSBsaXN0X2Zyb21hcnJheSh0ZXN0LCBzaXplb2YgdGVzdC9zaXplb2YgKnRlc3QpOwogICAgbGlzdF9wcmludChsaXN0LCAiZmluYWwgbGlzdCIpOwogICAgbGlzdF9mcmVlKGxpc3QpOwp9CgpzdHJ1Y3QgbGlzdCAqbGlzdF9mcm9tYXJyYXk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