B1097 Deduplication on a Linked

2020-06-14  本文已影响0人  Tsukinousag

B1097 Deduplication on a Linked List (25分)

注意点:

段错误发生在第二个链表可能为空链表

for(int i=0;i<vt2.size()-1;i++)//此处发生段错误

#include<iostream>
#include<map>
#include<bits/stdc++.h>
#include<vector>
#include<math.h>
#include<cmath>

using namespace std;

const int MAX=1e6+10;

struct Node
{
    int address,data,next;
}node[MAX];

vector<Node>vt,vt1,vt2;

map<int,int>mp;

int main()
{
    int n,start;
    scanf("%d%d",&start,&n);

    for(int i=0;i<n;i++)
    {
        int address;
        scanf("%d",&address);
        scanf("%d%d",&node[address].data,&node[address].next);
        node[address].address=address;
    }

    int p=start;
    while(p!=-1)
    {
        vt.push_back(node[p]);
        p=node[p].next;
    }

    for(int i=0;i<vt.size();i++)
    {
        if(mp[abs(vt[i].data)]==0)
        {
            vt1.push_back(vt[i]);
            mp[abs(vt[i].data)]=1;
        }
        else
            vt2.push_back(vt[i]);
    }

    for(int i=0;i<vt1.size()-1;i++)
    {
        printf("%05d %d %05d\n",vt1[i].address,vt1[i].data,vt1[i+1].address);
    }
    printf("%05d %d -1\n",vt1[vt1.size()-1].address,vt1[vt1.size()-1].data);

    if(vt2.size()==0)
        return 0;
    for(int i=0;i<vt2.size()-1;i++)
    {
        printf("%05d %d %05d\n",vt2[i].address,vt2[i].data,vt2[i+1].address);
    }
    printf("%05d %d -1\n",vt2[vt2.size()-1].address,vt2[vt2.size()-1].data);

    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读