PAT 1097 Deduplication on a Linked List

个人学习记录,代码难免不尽人意

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 (≤10 5 ) 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 10 4, 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

cpp 复制代码
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<set>
using namespace std;
struct node{
	int data;
	int address;
	int next; 
}Node[100010];

int main(){
  int first,n,k;
  scanf("%d %d",&first,&n);
  for(int i=0;i<n;i++){
  	 int address,data,next;
  	 scanf("%d%d%d",&address,&data,&next);
  	 node a;
  	 a.address=address;
  	 a.data=data;
  	 a.next=next;
  	 Node[address]=a;
  }
  

  set<int> s;
  s.insert(abs(Node[first].data));
  int now=Node[first].address;
  int newfirst=-1;
  int newtemp=-1;
  bool flag=true;
  while(Node[now].next!=-1){
  	node no=Node[now];
  	if(s.find(abs(Node[no.next].data))==s.end()){
  		s.insert(abs(Node[no.next].data));
  		now=no.next;
	  }
	else{
		Node[now].next=Node[no.next].next;
		if(flag){
			newfirst=no.next;
			newtemp=newfirst;
			flag=false;
		}
		else{
		Node[newtemp].next=no.next;
			newtemp=no.next;
			
		}
	
		
	}
  }
  now=Node[first].address;
  while(now!=-1){
  	if(Node[now].next==-1)
  		printf("%05d %d -1\n",Node[now].address,Node[now].data);
  	else 
	  printf("%05d %d %05d\n",Node[now].address,Node[now].data,Node[now].next);
  	now=Node[now].next;
  }
  if(newfirst!=-1){
  	Node[newtemp].next=-1;
  	int newnow=Node[newfirst].address;
  while(newnow!=-1){
  	if(Node[newnow].next==-1)
  		printf("%05d %d -1\n",Node[newnow].address,Node[newnow].data);
  	else 
	  printf("%05d %d %05d\n",Node[newnow].address,Node[newnow].data,Node[newnow].next);
  	newnow=Node[newnow].next;
  }
  }
  
}

本题真的踩了不少坑,我的做法和《算法笔记》略有不同,其中要注意的地方有1.注意边界,比如当前node的next值为-1,再让node=node.next就很有可能出错,应该事先判断。一定要注意边界值。并且,我的做法还要判断是不是有要删除的节点(即原链表中是否出现了两个绝对值一样的数),如果没有的话有些操作不应该出现。

相关推荐
liuluyang5301 小时前
C语言C11支持的结构体嵌套的用法
c语言·开发语言·算法·编译·c11
勤劳的进取家2 小时前
贪心算法之最小生成树问题
数据结构·python·算法·贪心算法·排序算法·动态规划
牛奶咖啡.8542 小时前
第十四届蓝桥杯大赛软件赛省赛C/C++ 大学 A 组真题
c语言·数据结构·c++·算法·蓝桥杯
亓才孓3 小时前
[leetcode]stack的基本操作的回顾
算法
小美爱刷题3 小时前
力扣DAY46-50 | 热100 | 二叉树:展开为链表、pre+inorder构建、路径总和、最近公共祖先、最大路径和
算法·leetcode·链表
Fanxt_Ja3 小时前
【数据结构】红黑树超详解 ---一篇通关红黑树原理(含源码解析+动态构建红黑树)
java·数据结构·算法·红黑树
永恒迷星.by3 小时前
全球变暖(蓝桥杯 2018 年第九届省赛)
算法
Dream it possible!4 小时前
CCF CSP 第35次(2024.09)(1_密码_C++)(哈希表)
c++·散列表·ccf csp·csp
旧时光林4 小时前
蓝桥杯 分解质因数(唯一分解定理)
数据结构·c++·算法·蓝桥杯·模拟·枚举
烁3474 小时前
每日一题(小白)模拟娱乐篇27
java·数据结构·算法·娱乐