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;
}
运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值