法1:
//先按输入顺序插入旧表
//后将正确指针顺序插入新表
#include <iostream>
using namespace std;
struct node
{
int address;
int data;
int next_address;
node* next;
};
node* Insert(node* L1,int tmpa,int tmpd,int tmpn){
node* tmp=new node;
tmp->address=tmpa;
tmp->data=tmpd;
tmp->next_address=tmpn;
tmp->next=NULL;
node* L=L1;
while(L->next){
L=L->next;
}
L->next=tmp;
return L1;
}
node* find_addr(node* L,int tmpa){
node* L1=L->next;
while(L1 && L1->address!=tmpa){
L1=L1->next;
}
return L1;
}
node* Order_Insert(node* L1,node* L2,int first_addr){
node* tmp=find_addr(L1,first_addr);
L2=Insert(L2,tmp->address,tmp->data,tmp->next_address);
while(tmp->next_address!=-1){
tmp=find_addr(L1,tmp->next_address);
L2=Insert(L2,tmp->address,tmp->data,tmp->next_address);
}
return L2;
}
int Length(node* L){
node* L1=L->next;
int num=0;
while(L1){
L1=L1->next;
num++;
}
return num;
}
void PrintL(node* L){
node* L1=L->next;
int flag=0;
while(L1){
if(flag){
if(L1->next_address!=-1){
printf("\n%05d %d %05d",L1->address,L1->data,L1->next_address);
}else{
printf("\n%05d %d %d",L1->address,L1->data,-1);
}
}else{
if(L1->next_address!=-1){
printf("%05d %d %05d",L1->address,L1->data,L1->next_address);
flag=1;
}else{
printf("%05d %d %d",L1->address,L1->data,-1);
flag=1;
}
}
L1=L1->next;
}
}
node* reverse(node* start,node* head,int K){
int cnt=1;
node* old=start;
node* cur=old->next;
node* nex=cur->next;
while(cnt < K){
cur->next=old;
cur->next_address=old->address;
old=cur;
cur=nex;
if(cur!=NULL)nex=cur->next;
cnt++;
}
head->next=old;head->next_address=old->address;
return cur;
}
int data[100005],nnext[100005],list[100005];
int main(){
//1:建立原表L1
node* L1=new node;
L1->next=NULL;
int first_addr,N,K;
scanf("%5d %d %d",&first_addr,&N,&K);
int tmpa,tmpd,tmpn;
for(int i=0;i<N;i++){
scanf("%5d %d %5d",&tmpa,&tmpd,&tmpn);
L1=Insert(L1,tmpa,tmpd,tmpn);
}
//2:建立有效表L2
node* L2=new node;
L2->next=NULL;
L2=Order_Insert(L1,L2,first_addr);
//PrintL(L2);
//3:反转表
int len2=Length(L2);
int cishu=len2/K;
if(cishu==0 || K==1){
PrintL(L2);
return 0;
}
node* start=L2->next;
node* end=start;
node* tmp;
node* head=L2;
for(int i=0;i<cishu;i++){
tmp=reverse(start,head,K);
head=start;
start->next=tmp;
if(tmp)start->next_address=tmp->address;
else start->next_address=-1;
start=tmp;
}
PrintL(L2);
return 0;
}
法2:
//插入有效表,使用数组
//别用cin,真的很慢
#include <iostream>
using namespace std;
struct node
{
int address;
int data;
int next_address;
node* next;
};
node* Insert(node* L1,int tmpa,int tmpd,int tmpn){
node* tmp=new node;
tmp->address=tmpa;
tmp->data=tmpd;
tmp->next_address=tmpn;
tmp->next=NULL;
node* L=L1;
while(L->next){
L=L->next;
}
L->next=tmp;
return L1;
}
node* find_addr(node* L,int tmpa){
node* L1=L->next;
while(L1 && L1->address!=tmpa){
L1=L1->next;
}
return L1;
}
node* Order_Insert(node* L1,node* L2,int first_addr){
node* tmp=find_addr(L1,first_addr);
L2=Insert(L2,tmp->address,tmp->data,tmp->next_address);
while(tmp->next_address!=-1){
tmp=find_addr(L1,tmp->next_address);
L2=Insert(L2,tmp->address,tmp->data,tmp->next_address);
}
return L2;
}
int Length(node* L){
node* L1=L->next;
int num=0;
while(L1){
L1=L1->next;
num++;
}
return num;
}
void PrintL(node* L){
node* L1=L->next;
int flag=0;
while(L1){
if(flag){
if(L1->next_address!=-1){
printf("\n%05d %d %05d",L1->address,L1->data,L1->next_address);
}else{
printf("\n%05d %d %d",L1->address,L1->data,-1);
}
}else{
if(L1->next_address!=-1){
printf("%05d %d %05d",L1->address,L1->data,L1->next_address);
flag=1;
}else{
printf("%05d %d %d",L1->address,L1->data,-1);
flag=1;
}
}
L1=L1->next;
}
}
node* reverse(node* start,node* head,int K){
int cnt=1;
node* old=start;
node* cur=old->next;
node* nex=cur->next;
while(cnt < K){
cur->next=old;
cur->next_address=old->address;
old=cur;
cur=nex;
if(cur!=NULL)nex=cur->next;
cnt++;
}
head->next=old;head->next_address=old->address;
return cur;
}
int data[100005],nnext[100005],list[100005];
int main(){
//1:建立原表L1
int first_addr,N,K;
//cin>>first_addr>>N>>K;
scanf("%5d %d %d",&first_addr,&N,&K);
int tmpa;
for(int i=0;i<N;i++){
scanf("%d",&tmpa);
scanf("%d %d",&data[tmpa],&nnext[tmpa]);
}
//2:建立有效表L2
node* L2=new node;
L2->next=NULL;
while (first_addr != -1) {
L2=Insert(L2,first_addr,data[first_addr],nnext[first_addr]);
first_addr = nnext[first_addr];
}
//PrintL(L2);
//3:反转表
int len2=Length(L2);
int cishu=len2/K;
if(cishu==0 || K==1){
PrintL(L2);
return 0;
}
node* start=L2->next;
node* end=start;
node* tmp;
node* head=L2;
for(int i=0;i<cishu;i++){
tmp=reverse(start,head,K);
head=start;
start->next=tmp;
if(tmp)start->next_address=tmp->address;
else start->next_address=-1;
start=tmp;
}
PrintL(L2);
return 0;
}
//两者差的不多,用数组存储快一点点点
//平均在4-6ms,还是达不到顺序表的速度。
//AC代码:
//顺序表:还是顺序表比较适合修改,链表插入的动作太耗时了
#include <iostream>
using namespace std;
int main() {
int first, k, n, sum = 0;
cin >> first >> n >> k;
int temp, data[100005], next[100005], list[100005], result[100005];
for (int i = 0; i < n; i++) {
cin >> temp;
cin >> data[temp] >> next[temp];
}
while (first != -1) {
list[sum++] = first;
first = next[first];
}
for (int i = 0; i < sum; i++) result[i] = list[i];
for (int i = 0; i < (sum - sum % k); i++)
result[i] = list[i / k * k + k - 1 - i % k];
for (int i = 0; i < sum - 1; i++)
printf("%05d %d %05d\n", result[i], data[result[i]], result[i + 1]);
printf("%05d %d -1", result[sum - 1], data[result[sum - 1]]);
return 0;
}