链表练习
来源:互联网 发布: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
- 写个内核链表,练习练习
- 链表练习
- 链表操作练习
- 链表练习
- 链表练习2
- 链表练习
- E2_链表练习
- 链表练习
- 链表练习
- 基础链表练习
- 链表练习
- 反转链表练习
- Leetcode__链表练习
- 链表练习
- 链表练习
- 链表练习
- 链表练习代码
- 简单链表练习
- 免费代理推荐 适合Windows PC电脑VPN,Mac Book苹果电脑,iPhone,IOS,Android 等手机平台,无需下载
- 如何恢复全局INDEX
- 整理笔记
- Spring Boot 快速入门
- monkey android win7安装使用
- 链表练习
- 动态规划 三角形最佳路径问题
- 解决int型变量cin输入字符无限循环问题
- python内置函数
- 点到点路径和问题(动态规划)
- 2017 小米笔试题 编程题 求树的高度 Java代码实现
- Badboy提示脚本错误解决方法
- Linux的基本指令
- linux下软件安装apt-get yum dpkg rpm 的使用