链表练习

来源:互联网 发布:centos 7网卡未连接 编辑:程序博客网 时间:2024/06/07 00:39
#include<iostream> #include<cmath>#include<string.h> #include<cstdio>#include<stdlib.h>#define LEN sizeof(struct stu)#define LL long longusing namespace std;struct stu{    int num;    struct stu *next;};struct stu *creat(int n){    struct stu *head;    struct stu *p;    struct stu *tail;    int x=1;    tail=head=NULL;    while(x<=n)    {        p=(struct stu *)malloc(LEN);        p->num=x;        if(head==NULL)            head=p;        if(tail!=NULL)            tail->next=p;        tail=p;        x++;        }    if(tail!=NULL)        tail->next=NULL;    return head;}void list(struct stu *head){    struct stu *p;    p=head;    do    {        printf("%d ",p->num);        p=p->next;    }while(p!=NULL);    printf("\n");}struct stu *move(int a,int x,int y,struct stu *head){    struct stu *px;    struct stu *py;    struct stu *p1;    struct stu *p2;    struct stu *p0;    struct stu *p=head;    p1=p2=px=py=head;    int pxx=1,pyy=1;    if(p->num==x)        pxx=0;    if(p->num==y)        pyy=0;    while(pxx==1||pyy==1)    {        if(p->next->num==x)        {            p1=p;            px=p->next;            pxx=0;        }        if(p->next->num==y)        {            p2=p;            py=p->next;            pyy=0;        }        p=p->next;    }    p=head;    if(a==1&&px!=p2&&px!=py)    {        if(px==head)        {            p=px->next;            p2->next=px;            px->next=py;        }        else        {            p1->next=px->next;            p2->next=px;            px->next=py;        }    }    if(a==2&&p1!=py&&px!=py)    {        if(px==head)        {            p=px->next;            px->next=py->next;            py->next=px;            }        else        {            p1->next=px->next;            px->next=py->next;            py->next=px;        }    }    if(a==3)    {        if(px==head)        {            p0=py->next;            py->next=px->next;            p2->next=px;            px->next=p0;            p=py;        }        else if(py==head)        {            p0=px->next;            px->next=py->next;            p1->next=py;            py->next=p0;            p=px;        }        else        {               p0=py->next;            p1->next=py;            p2->next=px;            py->next=px->next;            px->next=p0;        }    }    return p;}struct stu *reversal(struct stu *head){    struct stu *p;    struct stu *p1;    struct stu *p2;    p=head;    p1=head;    p2=head;    while(p!=NULL)    {        p2=p->next;        if(p==head)        {               p->next=NULL;        }         else        {            p->next=p1;        }        p1=p;        p=p2;    }     return p1;}LL sol(int a,struct stu *head){    LL ans=0;    struct stu *p;    p=head;    int i=1;    while(p!=NULL)    {        if(i%2==a)        {            ans+=p->num;            }        p=p->next;        i++;    }    return ans;}int main(){    int n,m,c=1;    while(scanf("%d%d",&n,&m)!=EOF)    {        struct stu *head;        head=creat(n);        //list(head);        int a,x,y;        int c4=0;        scanf("%d",&a);        while(m--)        {            if(a!=4)                scanf("%d%d",&x,&y);            if(a==1)            {                if(c4%2==1)                    head=move(2,x,y,head);                else                    head=move(1,x,y,head);            }            else if(a==2)            {                if(c4%2==1)                    head=move(1,x,y,head);                else                    head=move(2,x,y,head);            }            else if(a==3)            {                head=move(3,x,y,head);            }            else if(a==4)            {                c4++;                //head=reversal(head);            }            //list(head);            if(m!=0)                scanf("%d",&a);        }        LL ans=0;        if(c4%2==0)             ans=sol(1,head);        else        {            if(n%2==0)                ans=sol(0,head);            else                ans=sol(1,head);        }        printf("Case %d: %lld\n",c++,ans);        //list(head);    }    return 0;}
0 0
原创粉丝点击