#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 insMiddle(int n, int x){
int i;
Node *p,*q;
p=head;
for(i=1;i<n;i++){
p=p->next;
}
q=createN(x);
q->next=p->next;
p->next=q;
}
void delMiddle(int n){
int i;
Node *p,*q;
p=head;
for(i=1;i<n-1;i++){
p=p->next;
}
q=p->next;
p->next=q->next;
}
int main(void){
int i,n,x,y,z;
initL(n);
printL();
insMiddle(x,y);
printL();
delMiddle(z);
printL();
freeL();
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KIAp0eXBlZGVmIHN0cnVjdCBub2RlIHsKICAgIGludCB2YWw7CiAgICBzdHJ1Y3Qgbm9kZSAqbmV4dDsKfU5vZGU7CiAKTm9kZSAqaGVhZCA9IE5VTEw7CiAKTm9kZSogY3JlYXRlTihpbnQgeCl7CiAgICBOb2RlICpuZXdub2RlOwogICAgbmV3bm9kZSA9IChOb2RlICopbWFsbG9jKHNpemVvZihOb2RlKSk7CiAgICBuZXdub2RlLT52YWwgPSB4OwogICAgbmV3bm9kZS0+bmV4dCA9IE5VTEw7CiAgICByZXR1cm4gbmV3bm9kZTsKfQogCnZvaWQgaW5pdEwoaW50IG4pewogICAgaW50IHgsaTsKICAgIE5vZGUgKnA7CiAgICBzY2FuZigiJWQiLCZ4KTsKICAgIGhlYWQgPSBjcmVhdGVOKHgpOwogICAgcCA9IGhlYWQ7CiAgICBmb3IoaT0xO2k8bjtpKyspewogICAgICAgIHNjYW5mKCIlZCIs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