c/c++语言开发共享带有指向下一个结构的指针?

我想要一个会话,我在链接列表中的size变量中插入了10个不同的整数。 我相信我应该使用result_register()吗?

当存储所有10个整数时,我想通过输入result-> size打印出来。

我是链接列表的新手,所以请保持温和。

struct result { int size; }; struct result* result_next() { // What do I type here? } void result_register(int size) { // What do I type here? } 

主要

 struct result* result; while ((result = result_next())) printf("%dn", result->size); 

然后我希望能够通过如上所述打印出结果。

    你的结果应该有一个指向链表中下一个节点的next指针:

     struct result { int size; struct result *next; }; 

    然后,假设你有一个指针头,你只需走在列表中;

     struct result *node = head; while(node != null) { printf("%dn", node->size); node = node->next; } 

    要在列表中创建新项,您需要以下内容:

     struct result *Add(struct result *head, int size) { struct result *newHead = (struct result*)malloc(sizeof(struct result)); newHead->size = size; newHead->next = head; return newHead; } 

    现在你可以说:

     struct head *head=add(null, 0); head *head=add(head, 1); head *head=add(head, 2); head *head=add(head, 3); 

    这将为您提供一个链接列表,其中包含3 -> 2 -> 1 -> 0

      以上就是c/c++开发分享带有指向下一个结构的指针?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/518994.html

      (0)
      上一篇 2020年12月5日
      下一篇 2020年12月5日

      精彩推荐