1074 Reversing Linked List (25)

Posted mr-stn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1074 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


题目大意:给出n个节点,以及头结点,要求把相邻的k个节点翻转, 如果n不能被k整除,剩下的节点按照原来的顺序
思路:先用vector<int> value保存当前节点的值, vector<int> v保存当前节点指向下一个节点的指针; 然后再根据链表信息把k个链表的信息保存在vector<node> temp中, 然后在反向添加到vector<node> link中;
修改link中链表信息,输出结果
注意点:在使用vector的时候,如果事先申明了vector的大小,如果再使用push_back()在压在申请内存的后面的,而不是从头开始压入的;
    此外,pat的链表类题中,经常存在不在链表中的节点, 所以在最后的循环输出的时候,不能以节点个数作为循环结束的判断,应该以link的大小作为循环次数的判断
 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 struct node{
 5   int addr, val, next;
 6 };
 7 
 8 int main(){
 9   int root, n, k, i, j;
10   cin>>root>>n>>k;
11  vector<int> value(100000), v(100000);
12  int addr, val, next;
13   for(i=0; i<n; i++){
14     scanf("%d %d %d", &addr, &val, &next);
15     value[addr] = val;
16     v[addr] = next;
17   }
18   vector<node> link;
19   for(i=0; i<n/k+1; i++){
20       node nnode;
21       vector<node> temp;
22       for(j=0; j<k; j++){
23         if(root==-1) break;
24         nnode.addr=root;
25         nnode.val=value[root];
26         nnode.next=v[root];
27         root=v[root];
28         temp.push_back(nnode);
29       }
30       if(temp.size()==k) for(j=k-1; j>=0; j--) link.push_back(temp[j]);
31       else for(j=0; j<temp.size(); j++) link.push_back(temp[j]);
32   }
33 
34   for(i=0; i<link.size(); i++){
35       if(i!=link.size()-1){
36         link[i].next=link[i+1].addr;
37         printf("%05d %d %05d
", link[i].addr, link[i].val, link[i].next);
38       }else printf("%05d %d %d", link[link.size()-1].addr, link[link.size()-1].val, -1);
39   }
40   
41   return 0;
42 }

 








以上是关于1074 Reversing Linked List (25)的主要内容,如果未能解决你的问题,请参考以下文章

1074. Reversing Linked List (25)

PAT 1074 Reversing Linked List[链表][一般]

1074. Reversing Linked List (25)

1074. Reversing Linked List (25)

1074. Reversing Linked List (25)

1074 Reversing Linked List