#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 = p->next)
;
p
= (struct link
*)malloc(sizeof(struct link
)); p->data = data;
p->next = NULL;
}
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+bmV4dCA9IE5VTEw7CglpZihsc3QtPmhlYWQgPT0gTlVMTCl7CgkJbHN0LT5oZWFkID0gaXRlbTsKCQlsc3QtPnRhaWwgPSBsc3QtPmhlYWQ7Cgl9ZWxzZXsKCQlsc3QtPnRhaWwtPm5leHQgPSBpdGVtOwoJCWxzdC0+dGF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