1097. Deduplication on a Linked List (25)

时间:2021-10-21 19:46:51

1097. Deduplication on a Linked List (25)

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int address;
    int data;
    int next;
}node[100001];
struct node *aqueue[100001],*bqueue[100001];
int arear,afront,brear,bfront;
int key[10001];
int main()
{
    int head,n;
    scanf("%d %d",&head,&n);
    while(n--)
    {
        int a,d,nt;
        scanf("%d %d %d",&a,&d,&nt);
        node[a].address=a;
        node[a].data=d;
        node[a].next=nt;
    }
    int x=head;
    while(x!=-1)
    {
        if(!key[abs(node[x].data)])
        {
            aqueue[arear++]=&node[x];
            key[abs(node[x].data)]=1;
        }
        else
            bqueue[brear++]=&node[x];
        x=node[x].next;
    }
    while(afront<arear)
    {
        if(afront!=arear-1)
            printf("%05d %d %05d\n",aqueue[afront]->address,aqueue[afront]->data,aqueue[afront+1]->address);
        else
            printf("%05d %d -1\n",aqueue[afront]->address,aqueue[afront]->data);
        ++afront;
    }
    while(bfront<brear)
    {
        if(bfront!=brear-1)
            printf("%05d %d %05d\n",bqueue[bfront]->address,bqueue[bfront]->data,bqueue[bfront+1]->address);
        else
            printf("%05d %d -1\n",bqueue[bfront]->address,bqueue[bfront]->data);
        ++bfront;
    }
    return 0;
}