题意:给出两条链表的首地址以及若干个节点的的地址、数据、下一个节点的地址,求两条链表的首个共用节点的地址。如果两条链表没有共用节点,则输出-1。

思路:使用静态链表,首先遍历一遍第一个链表并进行标记。然后遍历第二个链表,并检查标记元素,得出结果,进行输出。

代码如下:

```cpp
//所用解法不涉及节点的数据及其地址,
//故不需要在节点中来存储

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1000000;

struct Node
{
//指向下一个地址,标记是否在第一个链表中
int next,flag;
};

Node datas[maxn];

int main()
{
int first1, first2, num;

//读取第一个链表的首地址,第二个链表的首地址,节点数目
scanf("%d %d %d", &first1, &first2, &num);

//获取节点数据
for (int i = 0;i < num;i++)
{
int add_temp, next_temp;
char data_temp;
scanf("%d %c %d", &add_temp, &data_temp, &next_temp);

datas[add_temp].next = next_temp;
}

//遍历第一个链表并进行标记
while (first1 != -1)
{
datas[first1].flag = 1;
first1 = datas[first1].next;
}

//遍历第二个链表,并由标记元素来寻找第一个共同节点
while (first2 != -1 && datas[first2].flag != 1)
{
first2 = datas[first2].next;
}

//输出结果
if (first2 == -1)printf("-1\n");
else printf("%05d\n", first2);

return 0;
}
```

PAT-链表-A1032 Sharing的更多相关文章

  1. PAT甲级——A1032 Sharing

    To store English words, one method is to use linked lists and store a word letter by letter. To save ...

  2. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  3. PAT Advanced 1032 Sharing(25) [链表]

    题目 To store English words, one method is to use linked lists and store a word letter by letter. To s ...

  4. PAT A1032 Sharing

    题意:给出两条链表的首地址以及若干节点的地址,数据,下一个节点的地址,求两条链表的首个共用节点的地址.如果两条链表没有共用节点,则输出-1.思路步骤1:由于地址的范围很小,因此可以直接用静态链表,但是 ...

  5. 【PAT】1032 Sharing (25)(25 分)

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

  6. PAT甲 1032. Sharing (25) 2016-09-09 23:13 27人阅读 评论(0) 收藏

    1032. Sharing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To store Engl ...

  7. pat链表专题训练+搜索专题

    本期题目包括: 1074:https://pintia.cn/problem-sets/994805342720868352/problems/994805394512134144 1052:http ...

  8. A1032. Sharing

    To store English words, one method is to use linked lists and store a word letter by letter. To save ...

  9. PAT 甲级 1032 Sharing

    https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 To store English words ...

随机推荐

  1. STM32CubeMX自建MDK工程的基本步骤

    根据需要调节各总线频率 最下方选项,√去掉,不用实时更新库,选择自己库所在路径就好. 点击左侧, 选择"Code Generator", 选择.c 和 .h文件不分开 最后,点击& ...

  2. 0级搭建类007-Ubuntu Desktop Linux安装 (18.04.2) 公开

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  3. 堆之*bin理解

    在程序运行中,使用bins结构对释放的堆块进行管理,以减少向系统申请内存的开销,提高效率. chunk数据结构 从内存申请的所有堆块,都使用相同的数据结构——malloc_chunk,但在inuse和 ...

  4. DFS-回溯与剪枝-C - N皇后问题

    C - N皇后问题 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N,求出有多少种合法的放 ...

  5. Acer4315笔记本CPU升级

    终于有时间升级一下不怎么用的旧笔记本Acer4315了.在计划升级前了解了一下,芯片组是GL960,支持可升级的CPU有: CM560 CM570 T1600 T1700 T2310 T2330 T2 ...

  6. C 基础 _Generic 泛型应用

    引言 - _Generic 用法简介 #include <stdio.h> #define TYPENAME_CASE(type) \ type: #type, #define TYPEN ...

  7. vue常用插件之打印功能、二维码插件、批量打印二维码

    vue实现打印的两种方法 vue实现批量打印二维码 (需安装二维码插件qrcodejs2) 一.vue-print-nb插件 1.安装: npm i vue-print-nb -S 2.全局注册(ma ...

  8. MySQL的联表查询

    MySQL的联表查询 首选:分析查询的字段来自哪些表 进而:确定交集 然后:确定判断的条件 比如:从student表 和 result表 查学号.考试名称.学时.考试日期.考试成绩 表1: 学号 考试 ...

  9. AcWing 6. 多重背包问题 III

    //f[i,j] 所有只从前i块能量石中选,且总体积恰好为j的方案数 #include <iostream> #include <algorithm> #include < ...

  10. MySQL用B+树做索引

    索引这个词,相信大多数人已经相当熟悉了,很多人都知道MySQL的索引主要以B+树为主,但是要问到为什么用B+树,恐怕很少有人能把前因后果讲述的很完整.本文就来从头到尾介绍下数据库的索引. 索引是一种数 ...