PTA 乙级 1025 反转链表

代码实现:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define X 100000

typedef struct
{
	int Address, Data, Next;
}LinkedList;

int main()
{
	LinkedList *a = (LinkedList*)malloc(sizeof(LinkedList) * X);
	int K, start_address, N;

	for (int i = 0; i < X; i++)a[i].Data = -1, a[i].Next = -1;
	scanf("%d %d %d", &start_address, &N, &K);
	if (K > N)K -= N;
	for (int i = 0; i < N; i++)
	{
		int address, data, next;

		scanf("%d %d %d", &address, &data, &next);
		a[address].Data = data, a[address].Next = next, a[address].Address = address;
	}

	LinkedList *b = (LinkedList*)malloc(sizeof(LinkedList)*N);
	int j = 0, count = 0;
	b[j++] = a[start_address];
	while (b[j - 1].Next != -1)b[j] = a[b[j - 1].Next], j++;
	count = j;
	
	LinkedList *c = (LinkedList*)malloc(sizeof(LinkedList)*N);
	int cnt = 0;
	for (int i = K - 1; i < count; i += K)
		for (int j = 0; j < K; j++)c[cnt++] = b[i - j];
	for (int i = cnt; i < count; i++)c[cnt++] = b[i];
	for (int i = 0; i < count; i++)
		if (i != count - 1)c[i].Next = c[i + 1].Address;
		else c[i].Next = -1;
	for (int i = 0; i < count; i++)
		if (i != count - 1)printf("%05d %d %05d\n", c[i].Address, c[i].Data, c[i].Next);
		else printf("%05d %d %d\n", c[i].Address, c[i].Data, c[i].Next);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值