02-线性结构3 Reversing Linked List
来自:PTA_数据结构_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
程序代码:
#include<stdio.h>
//#include<iostream>
#define MaxSize 100005
//using namespace std;
int main(){
int Data[MaxSize];
int Next[MaxSize];
int list[MaxSize];
int FirstAdd,N,K;
scanf("%d %d %d",&FirstAdd,&N,&K);
for(int i=0;i<N;i++){
int tmpAdd,tmpData,tmpNext;
scanf("%d %d %d",&tmpAdd,&tmpData,&tmpNext);
Data[tmpAdd] = tmpData;
Next[tmpAdd] = tmpNext;
}
int sum=0; // 累计有效结点数
while(FirstAdd!=-1){ // 当尾结点为 -1 时结束
list[sum++] = FirstAdd; // 记录所有Address
FirstAdd = Next[FirstAdd]; // 找下一个结点
}
for(int i=0;i<sum-sum%K;i+=K){ // 每 K 个结点一个区间
for(int j=0;j<K/2;j++){ // 反转链表
int t = list[i+j];
list[i+j] = list[i+K-j-1];
list[i+K-j-1] = t;
}
}
for(int i=0;i<sum-1;i++)
printf("%05d %d %05d\n",list[i],Data[list[i]],list[i+1]);
printf("%05d %d -1\n",list[sum-1],Data[list[sum-1]]);
return 0;
}
【待更新】其实应该也可以直接在链表上操作,不用引入一个大数组了。。。
看一下这种方法:
#include <stdio.h>
#include <stdlib.h>
typedef int Position;
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
Position Address;
ElementType Data;
Position Next;
PtrToNode Nextptr;
};
typedef PtrToNode List;
#define MaxSize 100000
struct Node arr[MaxSize];
List ReadList(Position firstAddr, int *length)
{
int i, list_size;
Position addr, pos;
List L, p, temp;
L = (List)malloc(sizeof(struct Node)); L->Nextptr = NULL;
p = L;
memset(&arr, -1, sizeof(struct Node));
for ( i = 0; i < (*length); ++i ) {
scanf("%d", &addr);
arr[addr].Address = addr;
scanf("%d %d", &arr[addr].Data, &arr[addr].Next);
}
list_size = 0;
pos = firstAddr;
while (arr[pos].Address != -1) {
p->Nextptr = &arr[pos];
p = p->Nextptr;
++list_size;
if (arr[pos].Next == -1)
break;
pos = p->Next;
}
*length = list_size;
temp = L; L = L->Nextptr; free(temp);
return L;
}
List Reverse(List L, int reverse_length)
{
List p1, p2, p3, rear;
p1 = L; p2 = L->Nextptr; p3 = p2->Nextptr;
while (reverse_length--) {
p2->Nextptr = p1;
p1 = p2;
p2 = p3;
if(p3) p3 = p3->Nextptr;
}
rear = L->Nextptr;
rear->Nextptr = p2;
L->Nextptr = p1;
return rear;
}
List ReverseList(List L, int length, int reverse_length)
{
if (reverse_length == 1)
return L;
int t;
List head, p, temp;
head = (List)malloc(sizeof(struct Node)); head->Nextptr = L;
p = head;
t = length / reverse_length;
while (t--) {
p = Reverse(p, reverse_length);
}
if (length % reverse_length == 0)
p->Nextptr = NULL;
temp = head;
head = head->Nextptr;
free(temp);
return head;
}
void PrintList( List L )
{
List p;
p = L;
while (p) {
if (!p->Nextptr)
printf("%.5d %d %d\n", p->Address, p->Data, -1);
else
printf("%.5d %d %.5d\n", p->Address, p->Data, p->Nextptr->Address);
p = p->Nextptr;
}
}
int main()
{
Position firstAddr;
int N, K;
List L;
scanf("%d %d %d", &firstAddr, &N, &K);
L = ReadList(firstAddr, &N);
L = ReverseList(L, N, K);
PrintList(L);
return 0;
}