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
Problem-solving
Because the input method is the current address, the current value, and the next address, you need to set the Node structure to save three data; you
cannot directly connect all Node nodes during input, so use vector to traverse the Node nodes at the same time. Put in vector;
1.Node structure
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=100010;
struct Node{
int address;
int data;
int next;
}List[MAXN];
2. Input function
Save the input Node in the List array, and put the connected Node into the vector type list in order during the second traversal;
int head,N,K;
Node Head;
vector<Node> list;
void input()
{
cin>>head>>N>>K;
Head.next=head;
for(int i=0;i<N;i++)
{
int address;
cin>>address;
cin>>List[address].data>>List[address].next;
List[address].address=address;
}
//创建vector把链表逐一push
int p=head;
while(p!=-1)
{
list.push_back(List[p]);
p=List[p].next;
}
}
3. Reverse execution
Calculate the number of times that need to be reversed, that is, the total number of linked lists / k;
use the reverse function to reverse k nodes;
output the address, value, and next address of the Node in sequence (that is, the address of the next Node);
void Reverse(){
int group= list.size()/K; //判断一共要循环几次
for(int i=0;i<group;i++)
{
reverse(list.begin()+i*K,list.begin()+i*K+K);
}
for(int i=0;i<list.size();i++)
{
printf("%05d %d ",list[i].address,list[i].data);
if(i!=list.size()-1) printf("%05d",list[i+1].address);
else printf("-1");
printf("\n");
}
}
4.main function
int main()
{
input();
Reverse();
}
Complete code
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXSIZE 1000010
#define Null -1
//列表保存地址的方法,按照顺序在列表中存放数据地址,
//按照需要的反转列表,则反转了地址,
//而由地址可得到对象,进而得到对象的data,原本的next不需要了,
//反转后next就为列表后一个地址
struct Node{
int data;
int next; //指向下一个的坐标
}node[MAXSIZE];
int List[MAXSIZE]; //列表中按顺序放
int main()
{
int First, n,k ;
cin>>First>>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; //记录List的大小
int p=First; //指向第一个节点
while(p!=-1)
{
List[j++]=p; //列表保存地址
//通过访问列表,即可得到地址,然后在node里面用地址得到数据,next为下一个数的地址
p=node[p].next; //指向下一个节点
}
int i=0;
while(i+k<=j) //反转顺序
{
reverse(&List[i],&List[i+k]);
//reverse函数——给定的两个指针之间的列表元素
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);
}
The
key is to put the linked list in a vector or array in order, and then call the reverse function to reverse every k elements;
How to write the reverse function
Ptr Reverse( Ptr head, int K )
{ cnt = 1;
new = head->next;
old = new->next;
while ( cnt < K ) {
tmp = old->next;
old->next = new;
new = old; old = tmp;
cnt++;
}
head->next->next = old;
return new;
}
Three variables, new, old, temp;
new holds the first inverted element, old holds the second inverted element, temp holds the second element after the second element ';
old.next points to new, At this time, temp saves the original next of old; new = old, old = temp; temp = old.next, continue to point old to new;
loop k-1 times, complete the inversion of k elements, and then point the head pointer to The last element of the reversal is old, that is, a reversal is completed;
int Reverse(int head, int k) //将k个数字反转
{
int count=1;
int New = node[head].next;
int Old = node[New].next;
int temp;
while(count<k){
temp=node[Old].next;
node[Old].next=New;
New = Old;
Old=temp;
count++;
}
node[node[head].next].next=Old;
return New;
}
Complete code
#include<iostream>
using namespace std;
#define MAXSIZE 1000010
#define Null -1
struct Node{
int data;
int next; //指向下一个对象
}node[MAXSIZE];
int Reverse(int head, int k) //将k个数字反转
{
int count=1;
int New = node[head].next;
int Old = node[New].next;
int temp;
while(count<k){
temp=node[Old].next;
node[Old].next=New;
New = Old;
Old=temp;
count++;
}
node[node[head].next].next=Old;
return New;
}
int main()
{
int First, n,k ;
cin>>First>>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 times=n/k;
int Head=MAXSIZE-2; //取列表中不用给的下标,保存头节点
int R=Head; //保存头指针
node[Head].next=First;
//循环使用reverse函数,并后移头指针
while(times--){
node[Head].next=Reverse(Head,k); //完成k个数的反转
for(int i=0;i<k;i++){
Head=node[Head].next; //指针后移k个元素
}
}
Head=R;
//输出
while(node[node[Head].next].next!=-1)
{
printf("%05d %d %05d\n",node[Head].next,node[node[Head].next].data,node[node[Head].next].next);
Head=node[Head].next;
}
printf("%05d %d -1",node[Head].next,node[node[Head].next].data);
}
It is more troublesome, and the linked list is not traversed first and placed in the container in order;
this method can learn the writing of the reverse function;