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

本文介绍了一种算法,用于解决链表中每K个元素进行翻转的问题。通过详细解析输入输出规格、示例代码及算法思路,读者可以了解到如何在给定的链表上实现这一操作。


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

算法思路

在这里插入图片描述

内容概要:本文介绍了基于贝叶斯优化的CNN-LSTM混合神经网络在时间序列预测中的应用,并提供了完整的Matlab代码实现。该模型结合了卷积神经网络(CNN)在特征提取方面的优势与长短期记忆网络(LSTM)在处理时序依赖问题上的强大能力,形成一种高效的混合预测架构。通过贝叶斯优化算法自动调参,提升了模型的预测精度与泛化能力,适用于风电、光伏、负荷、交通流等多种复杂非线性系统的预测任务。文中还展示了模型训练流程、参数优化机制及实际预测效果析,突出其在科研与工程应用中的实用性。; 适合人群:具备一定机器学习基基于贝叶斯优化CNN-LSTM混合神经网络预测(Matlab代码实现)础和Matlab编程经验的高校研究生、科研人员及从事预测建模的工程技术人员,尤其适合关注深度学习与智能优化算法结合应用的研究者。; 使用场景及目标:①解决各类时间序列预测问题,如能源出力预测、电力负荷预测、环境数据预测等;②学习如何将CNN-LSTM模型与贝叶斯优化相结合,提升模型性能;③掌握Matlab环境下深度学习模型搭建与超参数自动优化的技术路线。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注贝叶斯优化模块与混合神经网络结构的设计逻辑,通过调整数据集和参数加深对模型工作机制的理解,同时可将其框架迁移至其他预测场景中验证效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值