02-线性结构3 Reversing Linked List

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 Lbeing 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

Code:


#include <stdio.h>

#define MAX 100000

typedef struct{

    int data;

    int next;

}Node;

//计算在数组中的元素个数

int CountNum(Node *list,int plist);

//反转元素

int Reverse(Node *list,int num,int plist,int k);

//打印元素

void Print(Node *list,int plist);

int main(){

    Node list[MAX];

    int plist,n,k;

    scanf("%d%d%d",&plist,&n,&k);

    int addr,data,next;

    while(n>0){

        scanf("%d%d%d",&addr,&data,&next);

        list[addr].data=data;

        list[addr].next=next;

        n--;

    }

    int num=CountNum(list,plist);

    int newplist=Reverse(list,num,plist,k);

    Print(list,newplist);

    return 0;

}

int Reverse(Node *list,int num,int plist,int k){

    int preNode,curNode,nextNode;

    preNode=-1;

    curNode=plist;

    nextNode=list[curNode].next;

    int head=-1,lasthead;

    for (int i=0;i<num/k;i++){

        lasthead=head;

        head=curNode;

        for(int j=0;j<k;j++){

            list[curNode].next=preNode;

            preNode=curNode;

            curNode=nextNode;

            nextNode=list[curNode].next;

        }

        if (i==0) plist=preNode;

        else list[lasthead].next=preNode;

    }

    list[head].next = curNode; //不用逆转的剩余部分加上

    return plist;

}

int CountNum(Node *list,int plist){

    int c=1;

    while((plist=list[plist].next)!=-1) c++;

    return c;

}

void Print(Node *list,int plist){

    while((list[plist].next)!=-1){

        printf("%05d %d %d\n",plist,list[plist].data,list[plist].next);

        plist = list[plist].next;

    }

    printf("%05d %d %d\n",plist,list[plist].data,list[plist].next);

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,223评论 0 13
  • 走得时候 说什么都是多余 所有情绪都在目光里 而你 连头也没抬
    居戎阅读 2,555评论 0 0
  • 昨晚孩子又像往常一样,在临睡前到我房间里跟我聊天。昨晚聊的是MC的制作流程。整个制作过程足足讲了半个小时,...
    水到渠成111阅读 1,288评论 0 0
  • 一、利:1、民族自信力 2、在诗歌中寻找远方,鼓励自我,淡然面对。 3、纵向传承,知道文化的源头,与知识更好地融合...
    囚歌歌歌阅读 2,981评论 0 0
  • 经常,读了关于爱情的文章之后我总会觉得很孤独。因为是单身久了,看到了情侣的甜蜜和单身者的自白我都会被触动。我觉得我...
    洒落的阳光阅读 1,067评论 0 1

友情链接更多精彩内容