PAT1074 Reversing Linked List (25)詳細題解


 

02-1. Reversing Linked List (25)

時間限制
400 ms
內存限制
65536 kB
代碼長度限制
8000 B
判題程序
Standard
作者
CHEN, Yue

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

 

這題用二維數組高維代表首地址啊address,低維[address][0]儲存data,[address][1]則儲存next,犧牲空間換時間。

再使用維數組list進行reverse.

 1 int list[100010];
 2 int node[100010][2];
 3 
 4     int st,num,r;
 5     cin>>st>>num>>r;
 6     int address,data,next,i;
 7     for(i=0;i<num;i++)
 8     {
 9         cin>>address>>data>>next;
10         node[address][0]=data;
11         node[address][1]=next;
12     }

先輸入node值。如圖

address 00000  00100 12309 33218 68237 99999
data 4 1 2 3 6 5
next 99999 12309 33218 00000 -1 68237

 

 

 

值得說明的是:不需要按照順序輸入,到數組中后自然會對應數組的下標值即address。

因此上表中按照數組順序排序,無初始值的數組下標省略不列。

下面對list賦address.

int m=0,n=st;
while(n!=-1)
{
    list[m++]=n;
    n=node[n][1];
}
list 0 1 2 3 4 5
address 00100 12309 33218 00000 99999 68237

 

 

其中list[0]儲存的是st的address,list[1]儲存的是st->next的地址,以此類推。直到遇到address為-1.

1 i=0;
2 while(i+r<=m)
3 {
4     reverse(list+i,list+i+r);
5     i=i+r;
6 }

進行反轉,使用algorithm的reverse函數。

1 for (i = 0; i < m-1; i++)
2 {
3     printf("%05d %d %05d\n", list[i], node[list[i]][0], list[i+1]);
4 }
5 printf("%05d %d -1\n", list[i], node[list[i]][0]);

最后進行輸出,注意的是以int形式儲存的,如address中的00000實際儲存位置是0,故輸出時注意是要用五位數輸出。

完整AC代碼如下:

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int list[100010];
 5 int node[100010][2];
 6 int main()
 7 {
 8     freopen("F:\\in1.txt","r",stdin);
 9     int st,num,r;
10     cin>>st>>num>>r;
11     int address,data,next,i;
12     for(i=0;i<num;i++)
13     {
14         cin>>address>>data>>next;
15         node[address][0]=data;
16         node[address][1]=next;
17     }
18     int m=0,n=st;
19     while(n!=-1)
20     {
21         list[m++]=n;
22         n=node[n][1];
23     }
24     i=0;
25     while(i+r<=m)
26     {
27         reverse(list+i,list+i+r);
28         i=i+r;
29     }
30     for (i = 0; i < m-1; i++)
31     {
32         printf("%05d %d %05d\n", list[i], node[list[i]][0], list[i+1]);
33     }
34     printf("%05d %d -1\n", list[i], node[list[i]][0]);
35 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM