编程题 Reversing Linked List

题目来源

网易MOOC上的数据结构课程,第二周的一个小练习。

题目描述

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.
Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤10​5​​) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.
Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:

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

Sample Output:

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

思路描述

链表实现

乍一看对这个地址的存储方式整得有点懵,其实就是正常的链表。
地址是5位,所以可以创建一个105大小的数组来模拟内存的总空间,然后把节点输入到对应地址下标的元素上。

逆序

首先要找到需要逆序的区间,应该还是需要从头指针开始,遍历指定的长度,找到这一次逆序区间的结束位置,放置尾指针。
用两个指针
从起始位置向后遍历,逐个将next指针指向节点的前一个。对操作的每个节点,先把它指向它的前一个节点,然后用下一个节点迭代。可以注意到需要寄存前一个节点,也要寄存后一个节点。
在这里插入图片描述
注意第一个元素需要指向序列的下一个元素,序列的入口则需要指向逆序前的尾元素
在这里插入图片描述
这样就完成用了一次的逆序。

迭代

每完成一次逆序后,需要将尾指针后一个元素迭代为头指针,再从头指针开始搜索下一个区间。
注意先判断迭代后的头指针是否到末尾,搜索完成后判断区间是否完整。

细节

这个题目遇到两个细节实现问题

  1. 输出固定位数的整数,不足的在前补零,如00100;使用printf("%05d", num)实现
  2. 结尾输出特殊性问题;将跳出循环的判断放在循环体内执行(使用break),判断的同时执行特殊性代码(如不输出分隔符、结尾特殊格式等)

代码实现

代码欠优化,请轻喷

#include <iostream>
using namespace std;

typedef struct Node N, *PN;
struct Node {
	int data;
	int next;
}ll[100000];
int front, n, k;

int search(int pin) {
	int i = k-1;
	while(i-- && pin!=-1){
		pin = ll[pin].next;
	}
	return pin;
}

void rev(int* pin, int *plast) {
	int pp = ll[*plast].next;
	int p = *pin;
	int pn = ll[p].next;
	while (1) {
		ll[p].next = pp;
		pp = p;
		if (pp == *plast) {
			break;
		}
		p = pn;
		pn = ll[pn].next;
	}

	int tmp = *pin;
	*pin = *plast;
	*plast = tmp;
}

int main()
{
	cin >> front>> n >> k;
	for (int i = 0; i < n; ++i) {
		int p;
		cin >> p;
		cin >> ll[p].data >> ll[p].next;
	}
	
	int *p = &front;
	while (*p != -1) {
		int pl = search(*p);
		if (pl == -1) {
			break;
		}
		rev(p, &pl);
		p = &ll[pl].next;
	}

	while (1) {
		printf("%05d %d ", front, ll[front].data);
		front = ll[front].next;
		if (front == -1) {
			printf("-1\n");
			break;
		}
		printf("%05d\n", front);
	}
    return 0;
}
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

XZth__

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

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

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

打赏作者

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

抵扣说明:

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

余额充值