链表_A1032 Sharing (25 分)

https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920

/*
*链表的处理
*1.定义静态链表,结构体数组
*2.初始化falg为false
*3.从链表首地址begin遍历,并标记有效结点
*4.对结点排序,有效结点true大于false
*/
#include<iostream>
using namespace std;
#include<cstdio>
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 address1,address2,num;
    scanf("%d%d%d",&address1,&address2,&num);
    int address,next;
    char data;
    for(int i=0;i<num;i++) {
        scanf("%d %c %d",&address,&data,&next);
        node[address].data=data;
        node[address].next=next;
    }
    int pointer;
    for(pointer=address1;pointer != -1;pointer=node[pointer].next) {
        node[pointer].flag=true; //枚举第一条链表结点并置为true
    }
    for(pointer=address2;pointer != -1;pointer=node[pointer].next) {
        if(node[pointer].flag==true) break; //找到共用结点
    }
    if(pointer != -1) {
        printf("%05d\n",pointer);
    }else {
        printf("-1\n");
    }
    return 0;
}
posted @ 2019-08-18 08:53  2o2o  阅读(116)  评论(0编辑  收藏  举报