CHAPTER 04:EX 24

24. Create a struct that holds an int and a pointer to another
instance of the same struct. Write a function that takes
the address of one of these structs and an int indicating
the length of the list you want created. This function will
make a whole chain of these structs (a linked list), starting
from the argument (the head of the list), with each one
pointing to the next. Make the new structs using new,
and put the count (which object number this is) in the int.
In the last struct in the list, put a zero value in the pointer
to indicate that it’s the end. Write a second function that
takes the head of your list and moves through to the end,
printing out both the pointer value and the int value for
each one.

/*

*/

#ifndef SOLUTION24_H
#define  SOLUTION24_H

struct  Link {
    
int  count;
    Link
*  next;
};

void  Makelist (Link *  lnk,  int  length);
void  Headmove (Link *  lnk);

#endif      // SOLUTION24_H

 

 

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

void  Makelist (Link *  lnk,  int  length) {
    
static   int  i  =   1 ;
    
if  (length  ==   1 ) {
        lnk
-> next  =   0 ;
        lnk
-> count  =  i;
        
return ;
    }
    lnk
-> next  =   new  Link;
    lnk
-> count  =  i;
    i
++ ;
    Makelist (lnk
-> next, length  -   1 );
}

void  Headmove (Link *  lnk) {
    cout 
<<  lnk -> count  <<  endl;
    cout 
<<  lnk -> next  <<  endl;
    
if  (lnk -> next  ==   0 )
        
return ;
    Headmove (lnk
-> next);
}

 

 

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

int  main() {
    Link Test;
    Makelist (
& Test,  10 );
    Headmove (
& Test);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值