#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node *next;
}Node;
Node *head = NULL;
Node *tail = 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 printN(Node *a){
if(a
== NULL
) printf("NULL\n"); }
void printL(){
Node *p = head;
while(p != NULL){
p = p->next;
}
}
Node* getN(int n){
int i;
Node *p;
p = head;
for(i=1;i<n;i++) p = p->next;
return p;
}
int countL(){
int ret = 0;
Node *p = head;
while(p!=NULL){
p = p->next;
ret++;
}
return ret;
}
Node* searchX(int x){
Node *p;
for(p=head; p!=NULL; p=p->next){
if(p->val == x) break;
}
return p;
}
void insHead(int x){
Node *p; //1
p = createN(x); //1
p->next = head; //2
head = p; //3
}
void insMiddle(int n, int x){
int i;
Node *p,*q;
p = head; //1
for(i=1;i<n;i++){ //2
p = p->next; //2
}
q = createN(x); //3
q->next = p->next; //4
p->next = q; //5
}
void insTail(int x){
Node *p;
p = head; //1
if(p==NULL){
head = createN(x);
return;
}
while(p->next != NULL){ //2
p = p->next; //2
}
p->next = createN(x); //3
}
void delHead(){
Node *p;
p = head; //1
head = head->next; //2
}
void delMiddle(int n){
int i;
Node *p,*q;
p = head; //1
for(i=1;i<n-1;i++){ //2
p = p->next; //2
}
q = p->next; //3
p->next = q->next; //4
}
void delTail(){
Node *p;
p = head; //1
while(p->next->next != NULL){ //2
p = p->next; //2
}
p->next = NULL; //4
}
void push(int x){
Node *p=createN(x);
p->next=head;
head=p;
}
int pop(){
if(head==NULL){
return 0;
}
Node *p=head;
int value=p->val;
head=head->next;
return value;
}
void enqueue(int x){
Node *p=createN(x);
if(tail==NULL){
head=tail=p;
}else{
tail->next=p;
tail=p;
}
}
int dequeue(){
if(head==NULL){
return 0;
}
Node *p=head;
int value=p->val;
head=head->next;
if(head==NULL){
tail=NULL;
}
return value;
}
int main(void){
int s1,s2,s3,q1,q2,q3;
push(1);
push(2);
push(3);
s1 = pop();
s2 = pop();
s3 = pop();
printf("%d %d %d\n",s1
,s2
,s3
); enqueue(1);
enqueue(2);
enqueue(3);
q1 = dequeue();
q2 = dequeue();
q3 = dequeue();
printf("%d %d %d\n",q1
,q2
,q3
); freeL();
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KIAp0eXBlZGVmIHN0cnVjdCBub2RlIHsKICAgIGludCB2YWw7CiAgICBzdHJ1Y3Qgbm9kZSAqbmV4dDsKfU5vZGU7CiAKTm9kZSAqaGVhZCA9IE5VTEw7Ck5vZGUgKnRhaWwgPSBOVUxMOwogCk5vZGUqIGNyZWF0ZU4oaW50IHgpewogICAgTm9kZSAqbmV3bm9kZTsKICAgIG5ld25vZGUgPSAoTm9kZSAqKW1hbGxvYyhzaXplb2YoTm9kZSkpOwogICAgbmV3bm9kZS0+dmFsID0geDsKICAgIG5ld25vZGUtPm5leHQgPSBOVUxMOwogICAgcmV0dXJuIG5ld25vZGU7Cn0KIAp2b2lkIGluaXRMKGludCBuKXsKICAgIGludCB4LGk7CiAgICBOb2RlICpwOwogICAgc2NhbmYoIiVkIiwmeCk7CiAgICBoZWFkID0gY3JlYXRlTih4KTsKICAgIHAgPSBoZWFkOwogICAgZm9yKGk9MTtpPG47aSsrKXsKICAgICAgICBzY2FuZigiJWQiLCZ4KTsKICAgICAgICBwLT5uZXh0ID0gY3JlYXRlTih4KTsKICAgICAgICBwID0gcC0+bmV4dDsKICAgIH0KfQogCnZvaWQgZnJlZUwoKXsKICAgIE5vZGUgKnA7CiAgICB3aGlsZShoZWFkIT1OVUxMKXsKICAgICAgICBwID0gaGVhZC0+bmV4dDsKICAgICAgICBmcmVlKGhlYWQpOwogICAgICAgIGhlYWQgPSBwOwogICAgfQp9CiAKdm9pZCBwcmludE4oTm9kZSAqYSl7CiAgICBpZihhID09IE5VTEwpIHByaW50ZigiTlVMTFxuIik7CiAgICBlbHNlIHByaW50ZigiJWRcbiIsYS0+dmFsKTsKfQogCnZvaWQgcHJpbnRMKCl7CiAgICBOb2RlICpwID0gaGVhZDsKICAgIHdoaWxlKHAgIT0gTlVMTCl7CiAgICAgICAgcHJpbnRmKCIlZCAiLHAtPnZhbCk7CiAgICAgICAgcCA9IHAtPm5leHQ7CiAgICB9CiAgICBwcmludGYoIlxuIik7Cn0KIApOb2RlKiBnZXROKGludCBuKXsKICAgIGludCBpOwogICAgTm9kZSAqcDsKICAgIHAgPSBoZWFkOwogICAgZm9yKGk9MTtpPG47aSsrKSBwID0gcC0+bmV4dDsKICAgIHJldHVybiBwOwp9CiAKaW50IGNvdW50TCgpewogICAgaW50IHJldCA9IDA7CiAgICBOb2RlICpwID0gaGVhZDsKICAgIHdoaWxlKHAhPU5VTEwpewogICAgICAgIHAgPSBwLT5uZXh0OwogICAgICAgIHJldCsrOwogICAgfQogICAgcmV0dXJuIHJldDsKfQogCk5vZGUqIHNlYXJjaFgoaW50IHgpewogICAgTm9kZSAqcDsKICAgIGZvcihwPWhlYWQ7IHAhPU5VTEw7IHA9cC0+bmV4dCl7CiAgICAgICAgaWYocC0+dmFsID09IHgpIGJyZWFrOwogICAgfQogICAgcmV0dXJuIHA7Cn0KIAp2b2lkIGluc0hlYWQoaW50IHgpewogICAgTm9kZSAqcDsgICAgICAgIC8v77yRCiAgICBwID0gY3JlYXRlTih4KTsgLy/vvJEKICAgIHAtPm5leHQgPSBoZWFkOyAvL++8kgogICAgaGVhZCA9IHA7ICAgICAgIC8v77yTCn0KIAp2b2lkIGluc01pZGRsZShpbnQgbiwgaW50IHgpewogICAgaW50IGk7CiAgICBOb2RlICpwLCpxOwogICAgcCA9IGhlYWQ7ICAgICAgICAgICAvL++8kQogICAgZm9yKGk9MTtpPG47aSsrKXsgICAvL++8kgogICAgICAgIHAgPSBwLT5uZXh0OyAgICAvL++8kgogICAgfQogICAgcSA9IGNyZWF0ZU4oeCk7ICAgICAvL++8kwogICAgcS0+bmV4dCA9IHAtPm5leHQ7ICAvL++8lAogICAgcC0+bmV4dCA9IHE7ICAgICAgICAvL++8lQp9CiAKdm9pZCBpbnNUYWlsKGludCB4KXsKICAgIE5vZGUgKnA7CiAgICBwID0gaGVhZDsgICAgICAgICAgICAgICAvL++8kQogICAgaWYocD09TlVMTCl7CiAgICAgICAgaGVhZCA9IGNyZWF0ZU4oeCk7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgd2hpbGUocC0+bmV4dCAhPSBOVUxMKXsgLy/vvJIKICAgICAgICBwID0gcC0+bmV4dDsgICAgICAgIC8v77ySCiAgICB9CiAgICBwLT5uZXh0ID0gY3JlYXRlTih4KTsgICAvL++8kwp9CiAKdm9pZCBkZWxIZWFkKCl7CiAgICBOb2RlICpwOwogICAgcCA9IGhlYWQ7ICAgICAgICAgICAvL++8kQogICAgaGVhZCA9IGhlYWQtPm5leHQ7ICAvL++8kgogICAgZnJlZShwKTsgICAgICAgICAgICAvL++8kwp9CiAKdm9pZCBkZWxNaWRkbGUoaW50IG4pewogICAgaW50IGk7CiAgICBOb2RlICpwLCpxOwogICAgcCA9IGhlYWQ7ICAgICAgICAgICAvL++8kQogICAgZm9yKGk9MTtpPG4tMTtpKyspeyAvL++8kgogICAgICAgIHAgPSBwLT5uZXh0OyAgICAvL++8kgogICAgfQogICAgcSA9IHAtPm5leHQ7ICAgICAgICAvL++8kwogICAgcC0+bmV4dCA9IHEtPm5leHQ7ICAvL++8lAogICAgZnJlZShxKTsgICAgICAgICAgICAvL++8lQp9CiAKdm9pZCBkZWxUYWlsKCl7CiAgICBOb2RlICpwOwogICAgcCA9IGhlYWQ7ICAgICAgICAgICAgICAgICAgICAgICAvL++8kQogICAgd2hpbGUocC0+bmV4dC0+bmV4dCAhPSBOVUxMKXsgICAvL++8kgogICAgICAgIHAgPSBwLT5uZXh0OyAgICAg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