链表练习

#include<iostream> 
#include<cmath>
#include<string.h> 
#include<cstdio>
#include<stdlib.h>
#define LEN sizeof(struct stu)
#define LL long long
using 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值