[pta]02-线性结构3 Reversing Linked List (25分)

02-线性结构3 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 LLL 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\le N≤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 NNN 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^2.导致运行超时。看到了别人的方法,取消了排序的步骤,用数组模拟内存,数组序号代表地址。使用了后面的代码,但是对于有多余结点不在链表上的情况不理解。

#include <stdio.h>
#include <stdlib.h>
struct LNode{
    int Address;
    int Data;
    int Next;
}LNode;

void LRead(int L,struct LNode *LHead);
void LPrint(struct LNode *LHead,int L);
void LSort(struct LNode *LHead,int L,int Address1st);
int main(){ 
    int L,K,Address1st;
    int t;
    t=L;
    scanf("%d%d%d",&Address1st,&L,&K);
    struct LNode LHead[L];
    LRead(L,&LHead);
    LSort(&LHead,L,Address1st);
    LPrint(&LHead,L);
    system("pause");
    return 0;
}

void LRead(int L,struct LNode *LHead){
    int t = L;
    while(t!=0){
    scanf("%d%d%d",&LHead[L-t].Address,&LHead[L-t].Data,&LHead[L-t].Next);
        t--;
    }
    return;
}

LSort(struct LNode *LHead,int L,int Address1st){

    struct LNode tmpList[L];
    int i,j;
    for(i=0;i<L;i++){
        if(LHead[i].Address==Address1st){
            tmpList[0]=LHead[i];
        }
    }
    for(i=0;i<L;i++){
        if(tmpList[i].Next==-1)break;
        for(j=0;j<L;j++){
            if(LHead[j].Address==tmpList[i].Next){
                //printf("tmpList[%d+1]=LHead[%d]\n",i,j);
                tmpList[i+1]=LHead[j];
                break;
            }
        }
    }
    for(j=0;j<L;j++){
        LHead[j]=tmpList[j];
    }

}



void LPrint(struct LNode *LHead,int L){
    int i = L;
    while(i!=0){
        if(LHead[L-1].Next<0){
            printf("%05d %d %d\n",LHead[L-i].Address,LHead[L-i].Data,LHead[L-i].Next);
        }else{          
            printf("%05d %d %05d\n",LHead[L-i].Address,LHead[L-i].Data,LHead[L-i].Next);
        }
        i--;
    }
    return ;
}

测试例子

00100 6 3
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

00100 6 2
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
00100 6 3
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

00100 6 3
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

修改后

#include <stdio.h>
#include <stdlib.h>
#define MAXN 100001  

struct Node{   
    int Data;  
    int Next;  
};  
void Read(struct Node *Space,int N);
void LPrint(struct Node *Space,int Address1st);
int Reverse(struct Node *Space,int Address1st,int K,int N);


int main(){  
    int Address1st, N, K; 
    struct Node Space[MAXN]; 
    //printf("&Address1st, &N, &K\n");
    scanf("%d%d%d", &Address1st, &N, &K);
    Read(&Space,N);
    //LPrint(&Space,Address1st);
    Address1st=Reverse(&Space,Address1st,K,N);
    LPrint(&Space,Address1st);
    system("pause");
    return 0;  
}  


void Read(struct Node *Space,int N){
    int Address;
    while(N!=0){
        scanf("%d",&Address);
        scanf("%d%d",&Space[Address].Data,&Space[Address].Next);
        N--;
    }
    //printf("read end\n");
    return ;
}
void LPrint(struct Node *Space,int Address1st){
    int Address = Address1st;
    while(Space[Address].Next!=-1){
        printf("%05d %d %05d\n",Address,Space[Address].Data,Space[Address].Next);
        Address=Space[Address].Next;
    }
    printf("%05d %d %d\n",Address,Space[Address].Data,Space[Address].Next);

    return;
}
int Reverse(struct Node *Space,int Address1st,int K,int N){
    //printf("reverse start\n");
    int Address=Address1st,tempAddress,CellAddress;
    int n=N/K,i,j,x;
    int arr[n*K];
    //printf("Address1st:%d,K:%d,N:%d,n:%d\n",Address1st,K,N,n);
    for(j=0;j<n*K;j++){
            arr[j]=Address;
            Address=Space[Address].Next;
        }
    for(i=0;i<n;i++){

        //printf("i:%d\n",i);
        for(j=0;j<K;j++){
            //
            //printf("j:%d\n",j);
            x = i*K+j;
            if(j==0){

                if(n-i==1){
                    Space[arr[x]].Next=Space[arr[x+K-1]].Next;
                }else{
                    Space[arr[x]].Next=arr[x+2*K-1];
                }

            }else{
                Space[arr[x]].Next=arr[x-1];

            }
            //printf("%05d\t%d\t%d\n",arr[x],Space[arr[x]].Data,Space[arr[x]].Next);
            //system("pause");
        }

    }
    //printf("reverse end\n");
    Address1st=arr[K-1];
    return Address1st;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值