CHAPTER 04:EX 25

本文介绍了一个使用C++实现的链表结构,并将构造链表和遍历链表的功能封装为类成员方法,通过递归的方式完成链表的创建与遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

25. Repeat Exercise 24, but put the functions inside a struct
instead of using “raw” structs and functions.

#ifndef SOLUTION25_H
#define SOLUTION25_H

struct Link {
    
int count;
    Link
* next;
    
void Makelist (int length);
    
void Headmove ();
};



#endif    //SOLUTION24_H
/*
25. Repeat Exercise 24, but put the functions inside a struct
instead of using “raw” structs and functions.
*/

#include
<iostream>
#include
"solution25.h"
using namespace std;

void Link::Makelist (int length) {
    
static i = 1;
    
if (length == 1) {
        count 
= i;
        next 
= 0;
        
return;
    }
        count 
= i;
        next 
= new Link;
        i
++;
    next
->Makelist(length - 1);        //递归,注意这里的next->Makelist,如果成员函数想在这种情况下递归,那么就要这样用。
}

void Link::Headmove () {
    cout 
<< count << endl;
    cout 
<< next << endl;
    
if (next == 0)
        
return;
    next
->Headmove ();        //递归,注意这里的next->Makelist,如果成员函数想在这种情况下递归,那么就要这样用。
}
#include<iostream>
#include
"solution25.h"
using namespace std;

int main() {
    Link Test;
    Test.Makelist (
5);
    Test.Headmove();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值