// sennkei list
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node *next;
}Node;
Node *head = NULL;
Node* createN(int x){
Node *newnode;
newnode
= (Node
*)malloc(sizeof(Node
)); newnode->val = x;
newnode->next = NULL;
return newnode;
}
void initL(int n){
int x,i;
Node *p;
head = createN(x);
p = head;
for(i=1;i<n;i++){
p->next = createN(x);
p = p->next;
}
}
void freeL(){
Node *p;
while(head!=NULL){
p = head->next;
head = p;
}
}
void printL(){
Node *p = head;
while(p != NULL){
p = p->next;
}
}
void insTail(int x){
Node *p;
p = head; // 1
while(p->next != NULL){ // 2
p = p->next; // 2
}
p->next = createN(x); // 3
}
void delTail(){
Node *p;
p = head; // 1
while(p->next->next != NULL){ // 2
p = p->next; // 2
}
p->next = NULL; // 4
}
int main(void){
int i,n,x1;
initL(n);
printL();
insTail(x1);
printL();
delTail();
printL();
freeL();
return 0;
}
Ly8gc2VubmtlaSBsaXN0CiAKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KIAp0eXBlZGVmIHN0cnVjdCBub2RlIHsKICAgIGludCB2YWw7CiAgICBzdHJ1Y3Qgbm9kZSAqbmV4dDsKfU5vZGU7CiAKTm9kZSAqaGVhZCA9IE5VTEw7CiAKTm9kZSogY3JlYXRlTihpbnQgeCl7CiAgICBOb2RlICpuZXdub2RlOwogICAgbmV3bm9kZSA9IChOb2RlICopbWFsbG9jKHNpemVvZihOb2RlKSk7CiAgICBuZXdub2RlLT52YWwgPSB4OwogICAgbmV3bm9kZS0+bmV4dCA9IE5VTEw7CiAgICByZXR1cm4gbmV3bm9kZTsKfQogCnZvaWQgaW5pdEwoaW50IG4pewogICAgaW50IHgsaTsKICAgIE5vZGUgKnA7CiAgICBzY2FuZigiJWQiLCZ4KTsKICAgIGhlYWQgPSBjcmVhdGVOKHgpOwogICAgcCA9IGhlYWQ7CiAgICBmb3IoaT0xO2k8bjtpKyspewogICAgICAgIHNjYW5mKCIlZCIsJngpOwogICAgICAgIHAtPm5leHQgPSBjcmVhdGVOKHgpOwogICAgICAgIHAgPSBwLT5uZXh0OwogICAgfQp9CiAKdm9pZCBmcmVlTCgpewogICAgTm9kZSAqcDsKICAgIHdoaWxlKGhlYWQhPU5VTEwpewogICAgICAgIHAgPSBoZWFkLT5uZXh0OwogICAgICAgIGZyZWUoaGVhZCk7CiAgICAgICAgaGVhZCA9IHA7CiAgICB9Cn0KIAp2b2lkIHByaW50TCgpewogICAgTm9kZSAqcCA9IGhlYWQ7CiAgICB3aGlsZShwICE9IE5VTEwpewogICAgICAgIHByaW50ZigiJWQgIixwLT52YWwpOwogICAgICAgIHAgPSBwLT5uZXh0OwogICAgfQogICAgcHJpbnRmKCJcbiIpOwp9CiAKIAp2b2lkIGluc1RhaWwoaW50IHgpewogICAgTm9kZSAqcDsKICAgIHAgPSBoZWFkOyAgICAgICAgICAgICAgIC8vIDEKICAgIHdoaWxlKHAtPm5leHQgIT0gTlVMTCl7IC8vIDIKICAgICAgICBwID0gcC0+bmV4dDsgICAgICAgIC8vIDIKICAgIH0KICAgIHAtPm5leHQgPSBjcmVhdGVOKHgpOyAgIC8vIDMKfQogCnZvaWQgZGVsVGFpbCgpewogICAgTm9kZSAqcDsKICAgIHAgPSBoZWFkOyAgICAgICAgICAgICAgICAgICAgICAgLy8gMQogICAgd2hpbGUocC0+bmV4dC0+bmV4dCAhPSBOVUxMKXsgICAvLyAyCiAgICAgICAgcCA9IHAtPm5leHQ7ICAgICAgICAgICAgICAgIC8vIDIKICAgIH0KICAgIGZyZWUocC0+bmV4dCk7ICAgICAgICAgICAgICAgICAgLy8gMwogICAgcC0+bmV4dCA9IE5VTEw7ICAgICAgICAgICAgICAgICAvLyA0Cn0KIAogCmludCBtYWluKHZvaWQpewogICAgaW50IGksbix4MTsKICAgIHNjYW5mKCIlZCIsJm4pOwogICAgaW5pdEwobik7CiAgICBwcmludEwoKTsKICAgIHNjYW5mKCIlZCIsJngxKTsKICAgIGluc1RhaWwoeDEpOwogICAgcHJpbnRMKCk7CiAgICBkZWxUYWlsKCk7CiAgICBwcmludEwoKTsKICAgIGZyZWVMKCk7CiAgICByZXR1cm4gMDsKfQo=