Q2:1025 反转链表

输入样例:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

输出样例:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

参考答案:

#include<iostream>
using namespace std;

struct node{
    int data;
    int next;
};                 
int main()
{
    int L,n,k; 
	node  nodearray[100001];
	//创建链表 
    cin >> L >> n >> k;
    int next, adress, data; 
    for (int i = 0; i < n; i++)
    {
        cin >> adress >> data >> next;
        nodearray[adress].data = data;
        nodearray[adress].next = next;
    }                                                              
    nodearray[100000].next = L;
    
    int r = L, t = 100000, w = L, q = L, p = -1;     
    n = 0;                              
    while (r != -1)
    {
        r = nodearray[r].next; 
        n++;
        if (n % k == 0)
        {
            p = w;
            while (p != r)
            {
                q = nodearray[p].next;
                nodearray[p].next = nodearray[t].next;
                nodearray[t].next = p;
                p = q;
            }
            nodearray[w].next = r;
            t = w;
            w = nodearray[t].next;
        }
    }
    //输出链表
    p = nodearray[100000].next;     
	while (p != -1)
    {
        if (nodearray[p].next != -1)
        {
            printf("%05d %d %05d\n", p, nodearray[p].data, nodearray[p].next);
        }
        else
        {
            printf("%05d %d %d\n", p, nodearray[p].data, -1);
        }   
        p = nodearray[p].next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只天蝎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值