02-線性結(jié)構(gòu)3 Reversing Linked List

02-線性結(jié)構(gòu)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;

//計算在數(shù)組中的元素個數(shù)

int CountNum(Node *list,int plist);

//反轉(zhuǎn)元素

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; //不用逆轉(zhuǎn)的剩余部分加上

? ? 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);

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,920評論 0 13
  • 走得時候 說什么都是多余 所有情緒都在目光里 而你 連頭也沒抬
    居戎閱讀 345評論 0 0
  • 昨晚孩子又像往常一樣,在臨睡前到我房間里跟我聊天。昨晚聊的是MC的制作流程。整個制作過程足足講了半個小時,...
    水到渠成111閱讀 166評論 0 0
  • 一、利:1、民族自信力 2、在詩歌中尋找遠方,鼓勵自我,淡然面對。 3、縱向傳承,知道文化的源頭,與知識更好地融合...
    囚歌歌歌閱讀 429評論 0 0
  • 經(jīng)常,讀了關(guān)于愛情的文章之后我總會覺得很孤獨。因為是單身久了,看到了情侶的甜蜜和單身者的自白我都會被觸動。我覺得我...
    灑落的陽光閱讀 125評論 0 1

友情鏈接更多精彩內(nèi)容