PAT_A1097#Deduplication on a Linked List

PAT_A1097#Deduplication on a Linked List,第1张

概述Source: PAT A1097 Deduplication on a Linked List (25 分) Description: Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. T Source:

PAT A1097 Deduplication on a Linked List (25 分)

Description:

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 (≤) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer,and NulL is represented by −.

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 1,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 599999 -7 8765423854 -15 0000087654 15 -100000 -15 9999900100 21 23854
Sample Output:
00100 21 2385423854 -15 9999999999 -7 -100000 -15 8765487654 15 -1
Keys: 哈希映射 Attention: 注意keep和remo为空时,不能输出-1 Code:
 1 #include<cstdio> 2 #include<vector> 3 #include<cmath> 4 using namespace std; 5 const int M=1e5+@H_404_168@10; 6 int mp[M]={@H_404_168@0}; 7 struct node 8 { 9     int data;10     int adrs,rear;11 }link[M],t;12 13 int main()14 {15 #ifdef ONliNE_JUDGE16 #else17     freopen("Test.txt","r",stdin);18 #endif // ONliNE_JUDGE19 20     int first,n;21     scanf("%d%d",&first,&n);22     for(int i=@H_404_168@0; i<n; i++)23     {24         scanf("%d%d%d",&t.adrs,&t.data,&t.rear);25         link[t.adrs] = t;26     }27     vector<int> keep,remo;28     while(first != -@H_404_168@1)29     {30         if(mp[(int)abs(link[first].data)]==@H_404_168@0)31         {32             keep.push_back(first);33             mp[(int)abs(link[first].data)]=@H_404_168@1;34         }35         else36             remo.push_back(first);37         first = link[first].rear;38     }39     for(int i=@H_404_168@0; i<keep.size(); i++)40     {41         if(i!=@H_404_168@0)42             printf("%05d\n",keep[i]);43         printf("%05d %d ",keep[i],link[keep[i]].data);44     }45     if(keep.size())46         printf("-1\n");47     for(int i=@H_404_168@0; i<remo.size(); i++)48     {49         if(i!=@H_404_168@0)50             printf("%05d\n",remo[i]);51         printf("%05d %d ",remo[i],link[remo[i]].data);52     }53     if(remo.size())54         printf("-1\n");55 56 57     return @H_404_168@0;58 }
总结

以上是内存溢出为你收集整理的PAT_A1097#Deduplication on a Linked List全部内容,希望文章能够帮你解决PAT_A1097#Deduplication on a Linked List所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1022364.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存