CHAPTER 04:EX 25

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

你可能感兴趣的:(CHAPTER 04:EX 25)