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
解题
因为输入方式为当前地址,当前数值,下个地址,所以需要设定Node结构保存三个数据;
无法直接在输入时连接所有的Node结点,故使用vector,在遍历Node结点的同时放入vector中;
1.Node结构
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=100010;
struct Node{
int address;
int data;
int next;
}List[MAXN];
2.输入函数
将输入的Node保存在List数组中,二次遍历时将连接的Node按顺序放入vector类型的list中;
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执行
计算需要反转的次数,即总链表数/k;
利用reverse函数,反转k个node;
按顺序输出Node的地址,值,下一个地址(即下一个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函数
int main()
{
input();
Reverse();
}
完整代码
#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);
}
总结
关键在于把链表按顺序放在vector或者数组中,然后调用reverse函数,反转每k个元素;
reverse函数的写法
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;
}
三个变量,new,old,temp;
new保存第一个被反转的元素,old保存第二个被反转的元素,temp保存第二个元素的后一个元素‘;
old.next指向new,此时temp保存old原先的next;new=old,old=temp;temp=old.next,继续把old指向new即可;
循环k-1次,完成k个元素的反转,然后把头指针指向被反转的最后一个元素old,即完成一次反转;
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;
}
完整代码
#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);
}
比较麻烦,没有把链表先遍历一遍按顺序放在容器中;
这个方法可以学习reverse函数的写法;