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 (≤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
理解題意:
- 輸入的第一行很重要:三個數(shù)分別代表:首元素的地址,元素總個數(shù)N,前K個元素做逆序
接下來就是N行數(shù)據(jù)輸入,三個數(shù)分別代表,該元素地址,該元素,該元素指向的下個節(jié)點的地址 - 輸出就很容易理解了
我的答案:
// C++版本,更簡單一些
#include<iostream>
#include<stdio.h>
#include<algorithm> ///使用到reverse 翻轉函數(shù)
using namespace std;
#define MAXSIZE 1000010 ///最大為五位數(shù)的地址
struct node ///使用順序表存儲data和下一地址next
{
int data;
int next;
}node[MAXSIZE];
int List[MAXSIZE]; ///存儲可以連接上的順序表
int main()
{
int First, n, k;
cin>>First>>n>>k; ///輸入頭地址 和 n,k;
int Address,Data,Next;
for(int i=0;i<n;i++)
{
cin>>Address>>Data>>Next;
node[Address].data=Data;
node[Address].next=Next;
}
int j=0; ///j用來存儲能夠首尾相連的節(jié)點數(shù)
int p=First; ///p指示當前結點
while(p!=-1)
{
List[j++]=p;
p=node[p].next;
}
int i=0;
while(i+k<=j) ///每k個節(jié)點做一次翻轉
{
reverse(&List[i],&List[i+k]);
i=i+k;
}
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;
}
c版本:
// C 實現(xiàn)版本, 與老師講的一致
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100000
typedef int Ptr;
//結構數(shù)組模擬鏈表
struct node{
int key;
Ptr next;
}list[MaxSize];
//逆轉某一段含K個結點的鏈表
Ptr Reverse ( Ptr head, int K ) {
Ptr New, Old, Tmp; //New指向逆轉鏈表表頭,Old指向未逆轉表頭,Tmp記錄Old后一個結點
int cnt = 1;
New = head;
Old = list[New].next;
while ( cnt < K ) {
Tmp = list[Old].next; //記錄Old下一個結點,防止逆轉指針后丟失
list[Old].next = New; //逆轉指針
New = Old; Old = Tmp; //New,Old向后轉移一個
cnt++;
}
list[head].next = Old; //原來的第一個結點逆轉后變?yōu)樽詈笠粋€結點,并連接剩下未逆轉的元素
return New;
}
//判斷是否需要逆轉
int NeedReverse ( Ptr head , int K) {
int i;
for( i = 1; list[head].next != -1; head = list[head].next ) {
i++;
if ( i == K ) return 1; //還有K個或以上結點,需要逆轉
}
return 0; //不足K個結點,不需要逆轉
}
//逆轉整個鏈表
Ptr ReversingLinkedList( Ptr head , int K ) {
Ptr UnreversedHead = head; //未逆轉的鏈表的第一個結點
Ptr ListHead; //整個表的第一個結點
Ptr TempTail; //臨時表尾,用來連接下一段逆轉的鏈表
if ( NeedReverse( UnreversedHead, K ) ) { //第一次先判斷是否需要逆轉
ListHead = Reverse( UnreversedHead, K ); //記住逆轉后的整個鏈表的第一個結點
TempTail = UnreversedHead; //記錄此逆轉鏈表的表尾
UnreversedHead = list[TempTail].next; //記錄未逆轉鏈表的表頭
}
else //鏈表結點個數(shù)小于K,無需逆轉
return head;
while ( NeedReverse( UnreversedHead, K ) ) {
list[TempTail].next = Reverse( UnreversedHead, K ); //上一個逆轉鏈表的表尾與這個逆轉鏈表的表頭連接
TempTail = UnreversedHead;
UnreversedHead = list[TempTail].next;
}
return ListHead;
}
//輸出鏈表
void PrintLinkedList ( Ptr head ) {
Ptr temp = head;
for(; list[temp].next != -1; temp = list[temp].next)
printf("%05d %d %05d\n", temp, list[temp].key, list[temp].next);
printf("%05d %d %d\n", temp, list[temp].key, list[temp].next);
}
int main(){
Ptr ad, head;
int N, K;
scanf("%d %d %d", &head, &N, &K);
for(int i = 0; i < N; i++){
scanf("%d", &ad);
scanf("%d %d", &list[ad].key, &list[ad].next);
}
PrintLinkedList( ReversingLinkedList( head, K ) );
return 0;
}
答案解析
明天再補上??!