#include <stdio.h>
#include <stdlib.h>
struct link{
int data;
struct link *next;
};
struct list{
struct link *head;
struct link *tail;
};
struct link * add_tail(struct list *lst, int value){
struct link
*item
= (struct link
*)malloc(sizeof(struct link
)); item->data = value;
item->next = NULL;
if(lst->head == NULL){
lst->head = item;
lst->tail = lst->head;
}else{
lst->tail->next = item;
lst->tail = item;
}
return item;
}
struct link * insert(struct list *lst, int data){
struct link *p, *t;
if(lst->head == NULL){
p
= (struct link
*)malloc(sizeof(struct link
)); p->data = data;
p->next = NULL;
lst->head = p;
}else{
p = lst->head;
while(p->next)
p = p->next;
t
= (struct link
*)malloc(sizeof(struct link
)); t->data = data;
t->next = NULL;
p->next = t;
}
return p;
}
void print(struct list *lst){
struct link *p = lst->head;
while(p){
p = p->next;
}
}
int main(){
struct list lst;
lst.head = lst.tail = NULL;
int in, i;
for(i = 1; i <= 3; i++){
add_tail(&lst, i);
}
print(&lst);
insert(&lst, 5);
print(&lst);
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnN0cnVjdCBsaW5rewoJaW50IGRhdGE7CglzdHJ1Y3QgbGluayAqbmV4dDsKfTsKCnN0cnVjdCBsaXN0ewoJc3RydWN0IGxpbmsgKmhlYWQ7CglzdHJ1Y3QgbGluayAqdGFpbDsKfTsKCnN0cnVjdCBsaW5rICogYWRkX3RhaWwoc3RydWN0IGxpc3QgKmxzdCwgaW50IHZhbHVlKXsKCXN0cnVjdCBsaW5rICppdGVtID0gKHN0cnVjdCBsaW5rICopbWFsbG9jKHNpemVvZihzdHJ1Y3QgbGluaykpOwoJaXRlbS0+ZGF0YSA9IHZhbHVlOwoJaXRlbS0+bmV4dCA9IE5VTEw7CglpZihsc3QtPmhlYWQgPT0gTlVMTCl7CgkJbHN0LT5oZWFkID0gaXRlbTsKCQlsc3QtPnRhaWwgPSBsc3QtPmhlYWQ7Cgl9ZWxzZXsKCQlsc3QtPnRhaWwtPm5leHQgPSBpdGVtOwoJC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