Given a constant k and a single-linked table L, write a program that reverses every K-node in L. For example: Given L is 1→2→3→4→5→6,k 3, the output should be 3→2→1→6→5→4, and if K is 4, the output should be 4→3→2→1→5→6, that is, the last K elements do not invert.
Input format:
Each input consists of 1 test cases. The 1th row of each test case gives the address of the 1th node, the total number of nodes with positive integer N (<= 105), and the positive integer k (<=n), which is the number of sub-chain nodes that require inversion. The address of the node is a 5-bit nonnegative integer, and the null address is represented by-1.
Next there are n rows, each in the following format:
Address Data Next
Where address is a node location, data is the integer that the node holds, and next is the address of the next node.
Output format:
For each test case, sequentially output the inverted list with one row on each node, with the same format as the input.
Input Sample:
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
The problem of course is to set up a list node to save the address and the address of the subsequent node.
1, first of all, think how we find this node through the address of a node, it is easy to think of the method is that the address is his array subscript, so we need an array of nodes, in the input of this list of nodes, the address as subscript, the node is assigned to the corresponding subscript position, But this just establishes the relationship between the address and the data, they are discrete, and we have to save these nodes sequentially.
2, then here with a vector as a linked list, because the memory will explode when declaring an array, so that the vector will be better, here by the address to find the node, they string up.
3, finally we use a vector to save the inverted list, with the loop in addition to the last node of the next to modify the address of their next node.
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std
;
struct node{int address;
int data;
int next;
};
int main () {int n,first,k;
Vector<node> Shunxu;
Vector<node> reverse;
cin>>first>>n>>k;
Node N; Node addr[100000];
Linked list array for (int i=0;i<n;i++) {cin>>n.address>>n.data>>n.next; Addr[n.address]=n;
Assign the node to the position of the corresponding subscript} int nextaddress=first;
while (nextaddress! =-1) {//through Next as the subscript to find elements, added to the vector, update next continue to look for Shunxu.push_back (addr[nextaddress]);
nextaddress = Addr[nextaddress].next; } int size=shunxu.size ();
The input node may not be in the linked list, and the length of the linked list is recorded as int temp=k-1; while (temp<size) {//Invert list, each flip K, insufficient k does not reverse and exit loop for (int i=temp;i>temp-k;i--) {Reverse.push_b
ACK (Shunxu[i]);
} temp+=k; } for (int i=temp-k+1;i<size;i++)//will not be reversed at last, copied to the inverseThe linked list Reverse.push_back (Shunxu[i]) after the turn;
for (int i=0;i<size-1;i++) {//modify their next, change to the next element's address reverse[i].next=reverse[i+1].address;
printf ("%05d%d%05d\n", reverse[i].address,reverse[i].data,reverse[i].next);
} printf ("%05d%d%d\n", reverse[size-1].address,reverse[size-1].data,-1);
return 0; }
Topic Links:
https://www.patest.cn/contests/pat-b-practise/1025