PAT (Advanced Level) 1074. Reversing Linked List (25)

简单题。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;

const int maxn=100000+10;

struct Node
{
    char Address[10];
    char Data[100];
    char Next[10];
} node[maxn],tmp[maxn],pri[maxn];

int n,k;
map<string,int>m;
char StAdd[10];

void Copy(int a,int b)
{
    strcpy(tmp[a].Address,node[b].Address);
    strcpy(tmp[a].Data,node[b].Data);
    strcpy(tmp[a].Next,node[b].Next);
}

int main()
{
    scanf("%s%d%d",StAdd,&n,&k);
    m.clear();
    for(int i=0; i<n; i++)
    {
        scanf("%s%s%s",node[i].Address,node[i].Data,node[i].Next);
        m[node[i].Address]=i;
    }

    int now=m[StAdd];
    int kk=0;
    while(1)
    {
        Copy(kk,now);
        if(strcmp("-1",node[now].Next)==0) break;
        kk++;
        now=m[node[now].Next];
    }

    now=0; int c=0;
    while(now<kk+1)
    {
        if(now+k-1<kk+1)
        {
            for(int i=now+k-1; i>=now; i--)
            {
                strcpy(pri[c].Address,tmp[i].Address);
                strcpy(pri[c].Data,tmp[i].Data);
                c++;
            }
            now=now+k;
        }
        else
        {
            for(int i=now; i<kk+1; i++)
            {
                strcpy(pri[c].Address,tmp[i].Address);
                strcpy(pri[c].Data,tmp[i].Data);
                c++;
            }
            now=kk+1;
        }
    }

    for(int i=0; i<kk+1; i++)
    {
        printf("%s %s ",pri[i].Address,pri[i].Data);
        if(i<kk+1-1) printf("%s\n",pri[i+1].Address);
        else printf("-1\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zufezzt/p/5634261.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值