02-线性结构3 Reversing Linked List (25 分)
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 (≤105) 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
此题不应当使用动态链表,因为无法存储,也不应当使用struct节点的vector容器,虽然可以进行排序但不好输出,应当使用静态结构体数组,在用一个int的数组来依次存放节点的位置(可以看成指向节点的指针,因为给出了节点的地址),反转时只需要反转int数组,输出时使用反转后的节点位置来输出
C++ reverse https://blog.csdn.net/gl486546/article/details/78072719?locationNum=3&fps=1
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxsize 1000010
struct node // 定义一个结构体数组
{
int data;
int next;
}node[maxsize]; //结构体数组
int List[maxsize];
int main() {
int Adr, Data, Next; //地址 数据 下一个地址
int first, N, K, i;// 第一个地址 总的节点数 每k反转节点
scanf("%d%d%d", &first, &N, &K); //输入
for (i = 0; i < N; i++) //输入数据
{
scanf("%d%d%d", &Adr, &Data, &Next);
node[Adr].data = Data; //会出现为零的
node[Adr].next = Next;
}
int p = first; //第一个节点的地址
int j = 0;
while (p != -1) //将所有在链表上的结点存放在一个数组中,
{//如果指针没有指向末尾
List[j++] = p; //用int 数组依次存储节点的地址
p = node[p].next;
}
i = 0;
while (i + K <= j) //假设k为4,j为9 // 每k个进行反转并循环
{ //i+k=4
reverse(&List[i], &List[i + K]); //反转
i = i + K;//i = 4
}
for (i = 0; i < j - 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;
}
//
// Created by wangzhiyong on 18-9-16.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#define Max_Node_Num 100002
using namespace std;
typedef struct Node *NextNode;
struct Node
{
int Address;
int Data;
int Next;
};
void OutPut(vector<Node> List);
bool SortByM1( const Node &v1, const Node &v2);
int main()
{
vector<struct Node> List,List1;
int Address,Data,Next,N,K;
cin>>Address>>N>>K;
Node temp;
for(int i = 0;i<N;i++)
{
cin>>Address>>Data>>Next;
temp.Address = Address;
temp.Data = Data;
temp.Next = Next;
List.push_back(temp);
}
// OutPut(List);
// cout<<"************************"<<endl;
// cout<<"************************"<<endl;
sort(List.begin(),List.end(),SortByM1);
// OutPut(List);
// cout<<"************************"<<endl;
// cout<<"************************"<<endl;
int m = 0;
while(m+K <= N)
{
reverse(&List[m],&List[m+K]);
m = m+K;
}
OutPut(List);
return 0;
}
void OutPut(vector<Node> List)
{
for (int i = 0; i < List.size() - 1; i++) {
printf("%05d %d %05d\n", List[i].Address, List[i].Data, List[i+1].Address); // 按格式输出
}
printf("%05d %d -1\n", List[List.size()-1].Address, List[List.size()-1].Data);//最后一个节点的输出格式
}
bool SortByM1( const Node &v1, const Node &v2)//注意:本函数的参数的类型一定要与vector中元素的类型一致
{
return v1.Data < v2.Data;//升序排列
}
此代码无法通过后两个测试节点
最大N,最后剩K-1不反转 | 答案错误 | 264 ms | 4596KB |
6 | 有多余结点不在链表上 | 答案错误 |