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 (≤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 MAXLEN 100000 //长度<=五位非负整数
struct node {
    int data;
    int next;
};
 
int k,head; //k为全局变量,控制变化长度
 
struct node workArray [MAXLEN]; //声明结构体数组workArray
 
int Input(struct node  array[])
{
    int i,inputHead,inputLength;
    int index,data,next;
     
scanf("%d %d %d",&inputHead,&inputLength,&k);
for (i=0;i<inputLength;i++){
    scanf("%d %d %d",&index,&data,&next);
    array[index].data=data;
    array[index].next=next;
}
return inputHead;  

}
 
int count(int head,struct node array[])
{
    int i,cnt=1;
    i=head;
    while(array[i].next!=-1){
        cnt++;
        i=array[i].next;
    }
    return cnt;
}
void PrintList(int head,struct node array[])
{
    int idx=head;
    //利用-1控制循环
    while(array[idx].next!= -1){
        printf("%05d %d %05d\n",idx,array[idx].data,array[idx].next);
        idx=array[idx].next;
    }  
    printf("%05d %d %d",idx,array[idx].data,array[idx].next);
}
 
int ReverseList(struct node array[],int *head,int k)
{
    /*
    首先用count求链表长度,放到cnt中保存。每次执行cnt自身减掉k,如果cnt<k则不进行翻转
    然后使用ptr1 ptr2 ptr3 三个指针
    ptr1为当前节点 ptr2为下一个节点 将ptr1->ptr2改为ptr2->ptr1。因为ptr2中的next原有内容会丢失,故用ptr3保存ptr2的下一个节点
    执行完一次后,k个节点区间内,头尾互换。
    故lastend保存前一区块的末端,是上一区间的头节点
    nexthead即下一区块的头结点,同样也是该区块翻转完后的末端。于是提前用lastend=nexthead保存。
    */
    int cnt;
    if(k==1)
        return 0;
    cnt=count(*head,array);
    int i,ptr1,ptr2,ptr3,firstflag=0,nexthead=*head,lastend=1;//ptr1指当前指针,ptr2指下一个要指向ptr1的,ptr3指向还未做反转的下一个。
    while(cnt>=k){
//      printf("-------head=%d,nexthead=%d,cnt=%d\n",*head,nexthead,cnt);//for_test
        ptr1=nexthead;
        ptr2=array[ptr1].next;
        for(i=1;i<k;i++){
            ptr3=array[ptr2].next;
            array[ptr2].next=ptr1;
            ptr1=ptr2;
            ptr2=ptr3; //ptr1=ptr2
         
}  

 
array[nexthead].next=ptr3;//主要反转做完后,重新定义头尾节点的指向。
if(firstflag==0){
    lastend=nexthead;//故lastend保存前一区块的末端,是上一区间的头节点。
    *head=ptr1;//此时ptr1指向头结点。 !!!这是对地址的操作
     
}
//删去firstflag判断,程序超时。
else{
    array[lastend].next=ptr1;   //将先前转换的子链表与后来的头尾相连
    lastend=nexthead;    //重定义lastend
}
 
firstflag++;
nexthead=ptr2;
cnt-=k;
}

}
 
int main()
{
    head=Input(workArray);
    ReverseList(workArray,&head,k);
    PrintList(head,workArray);
}

算法思路

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值