B1074 Reversing Linked List (反转链

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

B1074 Reversing Linked List (25分)

struct node
{
        int address,data,next;
        int order//有时用来删去无用结点
}Node[MAX];
vector<node>vt;
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;
}
int index=vt.size()/k//反转节的个数
for(int i=0;i<index;i++)
{
      reverse(vt.begin()+k*i,vt.begin()+k*(i+1));  
}
for(int i=0;i<vt.size();i++)
{
    if(i!=vt.size()-1)//一直到倒数第2个结点都满足
       printf("%05d %d %05d\n",vt[i].address,vt[i].data,vt[i+1].address);
    else//最后一个结点
       printf("%05d %d -1\n",vt[i].address,vt[i].data);
}

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

using namespace std;

const int MAX=1e5+10;

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

vector<node> vt;

int main()
{
    int start,n,k;
    cin>>start>>n>>k;
    for(int i=0;i<n;i++)
    {
        int address,data,next;
        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;
    }
    int index=vt.size()/k;//有几节
    for(int i=0;i<index;i++)
    reverse(vt.begin()+k*i,vt.begin()+k*(i+1));

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

    return 0;
}

上一篇下一篇

猜你喜欢

热点阅读