1097. Deduplication on a Linked List (25)

Deduplication on a Linked List (25)
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


结题思路:

本来想用map去做,后来发现其实数据并不大,直接数组保存。
后面基本跟链表处理一致。
地址输出的时候,自己写了一个简单的int格式化型输出,结果超时,改成c的printf格式输出即可。

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct node{
       int value;
       int next;
};
node listA[100005];
bool visited[10004];//用来判断节点是否被访问过 
void Deduplication(int firstAdd,int& list2)//用list2保存移除的节点组成的链表的首地址 
{
     int add=firstAdd;
     int remainedPre=-1,removedPre=-1;//保存当前节点之前的节点 
     while(add!=-1)
     {
           if(!visited[abs(listA[add].value)])//未被访问过,即为当前值第一次遇到。
           {
              if(remainedPre!=-1)//存在前继 
                 listA[remainedPre].next=add;//将上一个节点的next指向当前地址 
              remainedPre=add;//将当前地址置为上一节点地址 
              visited[abs(listA[add].value)]=true; 
           }
           else
           {
              if(removedPre==-1)//被移除节点组成的链表list2为空,为list2添加第一个节点 
              {   
                 list2=add;//为List2附首地址 
                 removedPre=add;
              }
              else
              {
                 listA[removedPre].next=add;
                 removedPre=add;
              } 
           }
           add=listA[add].next;     
     }
     listA[remainedPre].next=-1;//尾节点的next置空 
     listA[removedPre].next=-1;
}
//void FormatedString(int s)//反正就5位数。。。T T。。。(调用以后出现超时,在改用printf输出后ac)
//{
//          cout<<s/10000;
//          s=s%10000;
//          cout<<s/1000;
//          s=s%1000;
//          cout<<s/100;
//          s=s%100;
//          cout<<s/10<<s%10;
//}
void outputList(int add)//链表输出 
{
     while(add!=-1)
     {
          //FormatedString(add);
          printf("%05d",add);
          cout<<" "<< listA[add].value<<" ";
          if(listA[add].next!=-1)
             printf("%05d",listA[add].next);
             //FormatedString(listA[add].next);
          else
             cout<<"-1";
          cout<<endl;
          add=listA[add].next;            
     }    
}
int main()
{
    int firstAdd,num;
    cin>>firstAdd>>num;
    int site,value,next;
    int output=firstAdd,removed=-1;
    for(int i=0;i<num;++i)
    {
        cin>>site;
        cin>>listA[site].value;
        cin>>listA[site].next;
    }
    for(int i=0;i<10005;++i)
        visited[i]=false;
    Deduplication(output,removed);
    outputList(output);
    outputList(removed);//若不存在可移除节点,不会输出 
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值