PTA 02-线性结构3 Reversing Linked List

本文介绍了一种算法,用于实现单向链表中每K个元素的逆序操作。通过构造辅助数组完成节点存储与排序,并利用标准模板库中的reverse函数实现逆序,最后输出逆序后的链表。

02-线性结构3 Reversing Linked List

原题连接
解题参考代码

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

思路

1.用node[Maxsize]数组,以addres为下标存储Node结点
2.创建List[Maxsize]数组,以下标为排序顺序,存储addres
3.通过数组的reverse函数进行每K项的反转
4.输出数组,保证最后一个数组输出的项+“-1”

代码

#include<iostream>
#include<algorithm>
using namespace std;
#define MaxSize 100001//N (≤10 5)
struct Node{
    int data;
    int next;
}node[MaxSize];

int main(){
    int startpos,N,K,addres;
    cin>>startpos>>N>>K;
    //addres作为index,node可以通过addres进行检索找到data和next
    for(int i=0;i<N;i++){
        cin>>addres;
        cin>>node[addres].data>>node[addres].next;
    }
    //List存储新的结点0-N,值为地址。通过地址可以找到node的data
    int List[MaxSize];
    int cur=0;
    addres=startpos;
    while(addres!=-1){
        List[cur++]=addres;
        addres=node[addres].next;
    }
    //用reverse函数 进行K次翻转
    int i=0;
    while(i+K<=cur){
        reverse(&List[i],&List[i+K]);
        i+=K;
    }
    for(i=0;i<cur-1;i++){
        printf("%05d %d %05d\n",List[i],node[List[i]].data,List[i+1]);
    }
    printf("%05d %d -1\n",List[i],node[List[i]].data);
    return 0;
}

注意

  1. int List[MaxSize];
  2. while(addres!=-1) while循环的退出点是最后的addres为-1
  3. printf("%05d %d -1\n",List[i],node[List[i]].data);无论是谁为最后输出,结尾都为-1,不能打成node[List[i]].next

知识点—reverse

参考博客

std::reverse

简介:将容器的序列变为当前的逆序。

函数原型

template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last);

在该题目中:使用的方法是
reverse(&List[i],&List[i+k])

std::reverse_copy

简介:
reverse_copy() 算法可以将源序列复制到目的序列中,目的序列中的元素是逆序的。

函数原型

template <class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy (BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);

使用实例:

#include <iostream>
#include <list>
#include <vector>
#include <algorithm>

using namespace std;

void print(int& ele)
{
	cout<<ele<<", ";
}

void main()
{
	int dim[]={1,2,3,4,5,6,7,8};
	vector<int> v1,v2;
	list<int> l1,l2;

	v1.assign(dim,dim+8);
	reverse(v1.begin(),v1.end());
	cout<<"vector v1: ";
	for_each(v1.begin(),v1.end(),print);
	cout<<endl;

	copy(v1.rbegin(),v1.rend(),back_inserter(l1));
	cout<<"list l1: ";
	for_each(l1.begin(),l1.end(),print);
	cout<<endl;

	reverse(v1.begin()+2,v1.end()-2);
	cout<<"vector v1: ";
	for_each(v1.begin(),v1.end(),print);
	cout<<endl;

	list<int>::iterator posf=l1.begin();
	list<int>::iterator pose=l1.end();
	advance(posf,2);
	advance(pose,-2);
	reverse(posf,pose);
	cout<<"list l1: ";
	for_each(l1.begin(),l1.end(),print);
	cout<<endl;

	reverse_copy(v1.begin(),v1.end(),back_inserter(v2));
	cout<<"vector v2: ";
	for_each(v2.begin(),v2.end(),print);
	cout<<endl;

	cout<<"list l1: ";
	reverse_copy(l1.begin(),l1.end(),ostream_iterator<int>(cout,", "));
	cout<<endl;
}

疑问

  1. int List[MaxSize];
    不知为啥,List[N]不行,求大神指导!!
题目描述 给定一个常数 $K$ 以及一个单链表 $L$,请编写程序将 $L$ 中每 $K$ 个结点反转。例如:给定 $L$ 为 1→2→3→4→5→6,$K$ 为 3,则输出应该为 3→2→1→6→5→4;如果 $K$ 为 4,则输出应该为 4→3→2→1→5→6,即最后不到 $K$ 个元素不反转。 输入格式 每个输入包含一个测试用例。每个测试用例第 1 行给出第 1 个结点的地址、结点总个数正整数 $N (\le 10^5)$、以及正整数 $K (\le N)$,即要求反转的子链结点的个数。结点的地址是 5 位非负整数,NULL 地址用 −1 表示。 接下来有 $N$ 行,每行格式为: Address Data Next 其中 Address 是结点地址;Data 是该结点保存的整数数据;Next 是下一结点的地址。题目保证给出的链表不为空。 输出格式 对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。 输入样例 00100 6 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 23333 2 33218 输出样例 00000 4 33218 33218 3 12309 12309 1 99999 99999 5 68237 68237 6 23333 23333 2 -1 题目分析 本题需要将链表中每 $K$ 个结点反转,可以采用迭代的方法,每次找到 $K$ 个结点,将这 $K$ 个结点反转,然后将这 $K$ 个结点的前驱结点指向反转后的第一个结点,将反转后的 $K$ 个结点的最后一个结点指向下一个要反转的结点,然后继续进行下一轮反转。 需要注意的是,如果链表长度不足 $K$,则不进行反转。 代码实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值