Reversing Linked List 逆转线性表

该博客讨论了一种针对线性表的逆转算法,每次逆转每K个元素的链接。输入包含链表首节点地址、节点总数N和K值。博主首先尝试用链表实现但遇到困难,后来通过数组实现,虽然简洁但空间效率不高。重点在于找到next为-1的节点作为输出序列尾,并理解逆转时需包括整数倍的K个元素。最后,通过传递数组地址解决参数不变问题。
摘要由CSDN通过智能技术生成

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

刚开始的时候尝试用链表去做,但是太难了没有做出来,后面百度了一下,发现大部分都是用数组完成的,用数组操作起来简单很多,但是会浪费很多空间,如果有机会还是用链表实现。

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
	int Address;//地址
	int Data;//数据
	int Next;//下一个节点的地址
}list;

void List(list a[], int N, int headAddress);//构造线性表
void Exchange(list a[], int i, int j);//交换两个数的值
void Reverse(list a[], int N, int K);//逆转线性表
void PrintList(list a[], int N);//输出线性表

int main(){
	int N, K, headAddress, true_num;
	scanf("%d %d %d", &headAddress, &N, &K);//单独处理第一行输入,获取第一个地址来找到第一个元素
	list a[N];
	for (int i = 0; i<N; i++)
		scanf("%d %d %d", &a[i].Address, &a[i].Data, &a[i].Next);
	List(a, N, headAddress);
	for (int i = 0; i<N; i++)//找到表尾,排除不在表上的多余元素,确定共要输出几个值
		if (a[i].Next == -1)
			true_num = i + 1;
	Reverse(a, true_num, K);
	PrintList(a, true_num);
	return 0;
}

void List(list a[], int N, int headAddress){
//先找到第一个节点
	for (int i = 0; i<N; i++) {
		if (a[i].Address == headAddress) {
			Exchange(a, i, 0);
			break;
		}
	}

	for (int i = 0; i<N; i++) {
		for (int j = i+1; j<N; j++) {
			if (a[i].Next == a[j].Address) {
				Exchange(a, i+1, j);
				break;
			}
		}
	}
}

void Exchange(list a[], int i, int j){
	list temp;
	temp.Address = a[i].Address;
	temp.Data = a[i].Data;
	temp.Next = a[i].Next;
	a[i].Address = a[j].Address;
	a[i].Data = a[j].Data;
	a[i].Next = a[j].Next;
	a[j].Address = temp.Address;
	a[j].Data = temp.Data;
	a[j].Next = temp.Next;
}

void Reverse(list a[], int N, int K){
//逆转表
	for (int i = 0; i<N/K; i++){
		for (int j = 0; j<K/2; j++) {
			Exchange(a, i*K+j, (i+1)*K-j-1);
		}
	}
	//把逆转完的表的节点Next域重新赋值
	for (int i = 0; i<N; i++) {
		a[i].Next = a[i+1].Address;
		if (i == N-1)
			a[i].Next = -1;
	}
}

void PrintList(list a[], int N){
	for (int i = 0; i<N-1; i++) {
		printf("%05d %d %05d\n", a[i].Address, a[i].Data, a[i].Next);
	}
	printf("%05d %d %d\n", a[N-1].Address, a[N-1].Data, a[N-1].Next);
}
  1. 需要注意的点是,输入时可能会有多余的点,并不在要输出的序列上,所以要找到next域为-1的点作为输出序列尾。
  2. 在逆转序列时,K=3时,1→2→3→4→5→6, 逆转为3→2→1→6→5→4,也就是说所有的部分都要逆转,最开始我以为只要逆转前K个元素,实际上整数倍的都要逆转,所以在逆转时的循环里要把条件设置成i<N/K,交换时数组下标也要乘以i倍的K。
  3. 调用Reverse函数时直接把数组的地址传进去,这样就可以避免实参没有变化的问题出现。
for (int i = 0; i<N/K; i++){
		for (int j = 0; j<K/2; j++) {
			Exchange(a, i*K+j, (i+1)*K-j-1);
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值