PAT——A1097 Deduplication on a Linked List(链表)

题目链接:

#include<utility>
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<stdio.h>
#include<cmath>
#include<stdlib.h>
using namespace std;
#define maxn 100010
const int table=1000010;
struct Node{
    int data,address,next;
    int order;
}node[maxn];
bool is[table]={false};
bool cmp(Node a,Node b)
{
    return a.order<b.order;
}
int main()
{
    memset(is,false,sizeof(is));
    for(int i=0;i<maxn;i++)
    {
        node[i].order=2*maxn;
    }
    int n,begin,address;
    scanf("%d%d",&begin,&n);
    while(n--)
    {
        scanf("%d",&address);
        scanf("%d%d",&node[address].data,&node[address].next);
        node[address].address=address;
    }
    int p=begin;
    int countValid=0,countRemoved=0;
    while(p!=-1)
    {
        if(!is[abs(node[p].data)])
        {
            is[abs(node[p].data)]=true;
            node[p].order=countValid++;
        }
        else
        {
           node[p].order=maxn+countRemoved++;
        }
        p=node[p].next;
    }
    sort(node,node+maxn,cmp);
    int count=countValid+countRemoved;
    for(int i=0;i<count;i++)
    {
        if(i!=count-1&&i!=countValid-1)
            printf("%05d %d %05d\n",node[i].address,node[i].data,node[i+1].address);
        else
        printf("%05d %d -1\n",node[i].address,node[i].data);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42232118/article/details/82118593
今日推荐