标签:创建 pre 代码 存在 style i++ bsp const iostream
1 #include<cstdio> 2 #include<iostream> 3 4 const int MAX = 1005; 5 using namespace std; 6 7 typedef struct node{ 8 int x; 9 int pre, next; 10 }Node; 11 12 //整个例子时间主要用于指向下一个节点的过程,代码:46行,66行 13 int main() 14 { 15 int n,m,p,q,i,j=0; 16 //在结构体中,学号既是数组下标也是值,所以说,其实值是多余的存在,这样子在查找符合对应学号的学生时, 17 //就避免了链表的查询速度慢的缺点 18 //使用结构体创建链表避免了数组移动速度慢的缺点 19 Node stu1[MAX]; 20 scanf("%d",&n); 21 stu1[0].pre = -1; 22 stu1[0].x = n; 23 stu1[0].next = 1;//通过首节点找到整条链表 24 for(i = 1;i <= n;i++)//初始化 25 { 26 stu1[i].x = i; 27 stu1[i].pre = i-1; 28 if(i+1 <= n)//结尾 29 stu1[i].next = i+1; 30 else 31 stu1[i].next = -1; 32 } 33 int pr,ne; 34 scanf("%d",&m); 35 while(m > 0) 36 { 37 scanf("%d %d",&p,&q); 38 j = p; 39 if(q > 0)//学号为p的同学向后移动q 40 { 41 pr = stu1[j].pre; 42 ne = stu1[j].next; 43 stu1[pr].next = stu1[j].next; 44 stu1[ne].pre = stu1[j].pre; 45 i = j; 46 while(q > 0) 47 { 48 j = stu1[j].next; 49 q--; 50 } 51 ne = stu1[j].next; 52 stu1[j].next = i; 53 stu1[i].pre = j; 54 stu1[i].next = ne; 55 if(ne != -1)//如果不是最后一个节点 56 stu1[ne].pre = i; 57 } 58 else//学号为p的同学向前移动-q 59 { 60 q = -q; 61 pr = stu1[j].pre; 62 ne = stu1[j].next; 63 stu1[pr].next = stu1[j].next; 64 stu1[ne].pre = stu1[j].pre; 65 i = j; 66 while(q > 0) 67 { 68 j = stu1[j].pre; 69 q--; 70 } 71 pr = stu1[j].pre; 72 stu1[j].pre = i; 73 stu1[pr].next = i; 74 stu1[i].pre = pr; 75 stu1[i].next = j; 76 } 77 m--; 78 } 79 i = 0; 80 i = stu1[i].next; 81 while(i != -1) 82 { 83 printf("%d ",stu1[i].x); 84 i = stu1[i].next; 85 } 86 87 return 0; 88 }
标签:创建 pre 代码 存在 style i++ bsp const iostream
原文地址:https://www.cnblogs.com/mlblog27/p/9534837.html