PTA 02-线性结构3 Reversing Linked List

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]不行,求大神指导!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值