PAT(甲级)渡劫(八)-Deduplication on a Linked List(25)

PAT(甲级)渡劫(八)-Deduplication on a Linked List(25)

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

struct node{
    int address;
    int data;
    int next;
}node[100001];

struct node *aqueue[100001],*bqueue[100001];
int arear,afront,brear,bfront;  // a,b两个队列的头尾指针
int key[100001];

int main(){
    //freopen("in.txt","r",stdin);
    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中,出现过的保存在bqueue中
                                    // key[]数组表示该数据是否重复出现
            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;
}

运行结果:

版权声明:本文为xr469786706原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xr469786706/article/details/87481852