Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (<= 105) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the position of the node, Key is an integer of which absolute value is no more than 104, and Next is the position of the next node.

Output Specification:

For each case, output the resulting linked list first, then the removed list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 5

99999 -7 87654

23854 -15 00000

87654 15 -1

00000 -15 99999

00100 21 23854

Sample Output:

00100 21 23854

23854 -15 99999

99999 -7 -1

00000 -15 87654

87654 15 -1

#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
struct node{
int id,data,next;
};
int main(){
int s,n,b=-1,index,check[10000]={0},t1=-1,last=-1,tag=0;
cin>>s>>n;
node list[100000];
for(int i=0;i<n;i++){
node nod;
cin>>nod.id>>nod.data>>nod.next;
list[nod.id]=nod;
}
index=s;
vector<node> single,del;
while(index!=-1){
if(check[abs(list[index].data)]==0)
single.push_back(list[index]);
else
del.push_back(list[index]);
check[abs(list[index].data)]=1;
index=list[index].next;
}
for(int i=0;i<single.size();i++){
if(i!=single.size()-1)
printf("%05d %d %05d\n",single[i].id,single[i].data,single[i+1].id);
else
printf("%05d %d %d\n",single[i].id,single[i].data,-1);
}
for(int i=0;i<del.size();i++){
if(i!=del.size()-1)
printf("%05d %d %05d\n",del[i].id,del[i].data,del[i+1].id);
else
printf("%05d %d %d\n",del[i].id,del[i].data,-1);
}
return 0;
}

PAT 1097. Deduplication on a Linked List (链表)的更多相关文章

  1. PAT甲级——1097 Deduplication on a Linked List (链表)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...

  2. PAT 1097 Deduplication on a Linked List[比较]

    1097 Deduplication on a Linked List(25 分) Given a singly linked list L with integer keys, you are su ...

  3. PAT (Advanced Level) Practise - 1097. Deduplication on a Linked List (25)

    http://www.patest.cn/contests/pat-a-practise/1097 Given a singly linked list L with integer keys, yo ...

  4. PAT Advanced 1097 Deduplication on a Linked List (25) [链表]

    题目 Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplica ...

  5. PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作

    给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...

  6. PAT A1097 Deduplication on a Linked List (25 分)——链表

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...

  7. 【PAT甲级】1097 Deduplication on a Linked List (25 分)

    题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...

  8. PAT (Advanced Level) 1097. Deduplication on a Linked List (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  9. 1097 Deduplication on a Linked List

    题意: 给出一个链表,删除绝对值相同的结点,对于每个绝对值为K的结点,只保留第一次出现的那个.把被移除的结点组成一个新链表,输出删除去重后的链表和新链表. 思路:考察链表的“删除”操作,不难. 代码: ...

随机推荐

  1. Linux下的ssh实验环境搭建与管理

    实验环境[size=10.5000pt]1:网桥模式[size=10.5000pt]2:安装好vmtoos[size=10.5000pt]3:安装好yum[size=10.5000pt]4:安装好ss ...

  2. code+3月赛 loj6299 白金元首与克劳德斯

    千里白金雪满天 烽火江山起狼烟 分手竟兵刃相见 1941.7. 苏联军队出乎意料的反抗力量.前线德军的补给困难 —— 元首 Adolf 望着天空的云层陷入沉思…… 在 xyxyxy-直角坐标平面的天空 ...

  3. E20170919-hm

    infinity   n. <数>无穷大; 无限的时间或空间;

  4. yii2表单,用惯yii1的可以看一下,有很大不同哦

    使用表单 本章节将介绍如何创建一个从用户那搜集数据的表单页.该页将显示一个包含 name 输入框和 email 输入框的表单.当搜集完这两部分信息后,页面将会显示用户输入的信息. 为了实现这个目标,除 ...

  5. java 重载父类报错 Remove '@override' annotation解决办法

    Remove '@override' annotation解决办法      最近刚刚配置了新机器,将原来的代码放在eclipse上执行,总会出现Remove '@override' annotati ...

  6. UNIX环境高级编程--6

    系统数据文件和信息    数据文件都是ASCII文本文件,并且使用标准I/O库读这些文件,例如口令文件/etc/passwd和组文件/etc/group就是经常被多个程序频繁使用的两个文件.    口 ...

  7. easyui datagrid 高度布局自适应

    最近在把以前写的一个项目改成用easyui做前端.过程中遇到了不少问题.其中一个就是datagrid不能很好的布局.想了好多办法都有局限.最后想到会不会是布局(easyui-layout)的问题,经过 ...

  8. 正则表达式 \D 元字符

    \D元字符可以匹配非数字字符,等价于"[^0-9]". 语法结构: (1).构造函数方式: new RegExp("\\D") (2).对象直接量方式: /\D ...

  9. HTML和CSS网页开发基础

    一 HTML文档结构 HTML文档结构:<html>.<head>.<title>.<body>构成HTML页面中最基本的元素. HTML常用标记:1. ...

  10. 关于idea failed to start SceneBuilder 的解决方法

    问题描述: javaFx无法启动SceneBuilder. 问题原因: SceneBuilder不正当配置. 解决方法:1. 下载SceneBuilder   https://www.oracle.c ...