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 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 (≤) which is the total number of nodes, and a positive K (≤) 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

天哪!一个链表写一天

 

 

#include <iostream>
#include <stdio.h>
using namespace std;

struct Node
{
    int Num;
    int Next; //下一个地址
}List[100001]; //地址100000作为开头

void print(int n)
{
    if(n==-1) {
        cout<<-1;
        return ;
    }
    int t=10000;
    while(t)
    {
        cout<<n/t;
        n=n%t;
        t=t/10;
    }
}

int main()
{
    int fir,N,K;
    scanf("%d %d %d",&fir,&N,&K);
    //cin>>fir>>N>>K;
    List[100000].Next=fir;
    for(int i=0;i<N;++i)
    {
        int t1,t,t2; //cin>>t1>>t>>t2;
        scanf("%d %d %d",&t1,&t,&t2);
        List[t1].Num=t;
        List[t1].Next=t2;
    }
    int P1=List[fir].Next,P2=List[fir].Next,P3=fir,P4=100000;
    while(1)
    {//cout<<"P4="<<P4<<" P3="<<P3<<" P2="<<P2<<" P1="<<P1<<endl;
        int flag=1;
        for(int i=1;i<K;i++)
        {
            if(P1!=-1)
            {
                P1=List[P1].Next;
                List[P2].Next=P3;
                P3=P2;
                P2=P1;
            }
            else
            {
                flag=0;
                break;
            }
            //cout<<"aaP4="<<P4<<" P3="<<P3<<" P2="<<P2<<" P1="<<P1<<endl;
        }
        if(flag)
        {
            int y=List[P4].Next;
            List[P4].Next=P3;
            P3=y;
            //cout<<"P3="<<P3<<endl;
            //for(int j=1;j<K;++j) P3=List[P3].Next;
            List[P3].Next=P1;
            //复原
            P4=P3;
            P3=P2;
            if(P2==-1) break; //刚好结束
            P1=List[P1].Next;
            P2=List[P2].Next;
            //cout<<"P4="<<P4<<" P3="<<P3<<" P2="<<P2<<" P1="<<P1<<endl;
        }
        else//结束,有尾巴,撤回操作(debug:多退了一步,啊啊啊啊阿)
        {
            while(P3!=List[P4].Next)//!!!
            {
                //P1=P2;
                P2=P3;
                P3=List[P3].Next;
                List[P2].Next=P1;
                P1=P2;
            }
            break;
        }
    }
    //cout<<endl;
    int P=100000;
    while(1)
    {
        P=List[P].Next;
        print(P);
        cout<<" "<<List[P].Num<<" ";
        print(List[P].Next);
        cout<<endl;
        if(List[P].Next==-1) break;
    }


    return 0;
}

 

 

 

posted @ 2020-03-11 22:45  liuyongliu  阅读(268)  评论(0编辑  收藏  举报