数据结构专题——链表处理 A1032.Sharing (静态链表)
#include <bits/stdc++.h> #include<math.h> #include <string> using namespace std; const int maxn = 100010; struct Node{//定义静态链表 char data; int next; bool flag; }node[maxn]; int main(){ for(int i =0;i<maxn;++i){ node[i].flag = false; } int s1,s2,n;//s1和s2分别代表两条链表的首地址 scanf("%d%d%d",&s1,&s2,&n); int address,next;//节点地址与后继节点地址 char data; for(int i=0;i<n;++i){ scanf("%d %c %d",&address,&data,&next); node[address].data = data; node[address].next = next; } int p; for(p = s1;p != -1;p = node[p].next){ node[p].flag = true;//枚举第一条链表的所有节点,令其出现次数为1 } for(p = s2;p != -1;p = node[p].next){ if(node[p].flag == true){ break; } } if(p != -1){ printf("%05d\n",p); }else{ printf("-1\n"); } system("pause"); return 0; }