• 自引用结构--之链表创建


    一、自引用结构,结构中有一个指向结构本身的指针:

      1、自引用结构的应用:

     1 //This is c program code!
     2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
     3   * 文档信息: *** :~/testTmp.c
     4   * 版权声明: *** :(魎魍魅魑)MIT
     5   * 联络信箱: *** :guochaoxxl@163.com
     6   * 创建时间: *** :2020年11月28日的上午10:18
     7   * 文档用途: *** :数据结构与算法分析-c语言描述
     8   * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl)
     9   * 修订时间: *** :2020年第47周 11月28日 星期六 上午10:18 (第333天)
    10   * 文件描述: *** :自行添加
    11  * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/
    12 #include <stdio.h>
    13 #include <string.h>
    14 #include <stdlib.h>                                                                    
    15 
    16 typedef struct _stu{
    17     char name[10];
    18     struct _stu *next;
    19 } Stu;
    20 
    21 int main(int argc, char **argv)
    22 {
    23     Stu *head;
    24     Stu *tmp;
    25     head = tmp = (Stu *)malloc(sizeof(Stu));
    26     tmp->next = NULL;
    27 
    28     char *stuName[] = {"lina", "mina", "bina", "tina", "dina"};
    29     int size = sizeof(stuName)/sizeof(stuName[0]);
    30 
    31     for(int i = 0; i < size; i++){
    32         strcpy(tmp->name, stuName[i]);
    33         Stu *tmpN = (Stu *)malloc(sizeof(Stu));
    34         tmpN->next = NULL;
    35         tmp->next = tmpN;
    36         tmp = tmpN;
    37     }
    38 
    39     for(int i = 0; i < size; i++){
    40         printf("%s
    ", head->name);
    41         head = head->next;
    42     }
    43 
    44     return 0;
    45 }

    与代码:

     1 //This is c program code!
     2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
     3   * 文档信息: *** :~/testTmp1.c
     4   * 版权声明: *** :(魎魍魅魑)MIT
     5   * 联络信箱: *** :guochaoxxl@163.com
     6   * 创建时间: *** :2020年11月28日的上午10:43
     7   * 文档用途: *** :数据结构与算法分析-c语言描述
     8   * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl)
     9   * 修订时间: *** :2020年第47周 11月28日 星期六 上午10:43 (第333天)
    10   * 文件描述: *** :自行添加
    11  * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/
    12 #include <stdio.h>
    13 #include <stdlib.h>
    14 #include <string.h>
    15 
    16 struct _stu{
    17     char name[10];
    18     struct _stu *next;
    19 };
    20 
    21 typedef struct _stu Stu;
    22 
    23 void display(Stu *head){
    24     int flag = 1;
    25 
    26     do{
    27         printf("%s
    ", head->name);                                                    
    28         if(NULL == head->next){
    29             flag = 0;
    30         }
    31         head = head->next;
    32     }while(flag);
    33 }
    34 
    35 int main(int argc, char **argv)
    36 {
    37     Stu *head;
    38 
    39     head = (Stu *)malloc(sizeof(Stu));
    40     strcpy(head->name, "lina");
    41     head->next = (Stu *)malloc(sizeof(Stu));
    42     strcpy(head->next->name, "mina");
    43     head->next->next = (Stu *)malloc(sizeof(Stu));
    44     strcpy(head->next->next->name, "bina");
    45     head->next->next->next = (Stu *)malloc(sizeof(Stu));
    46     strcpy(head->next->next->next->name, "tina");
    47     head->next->next->next->next = (Stu *)malloc(sizeof(Stu));
    48     strcpy(head->next->next->next->next->name, "dina");
    49     head->next->next->next->next->next = NULL;
    50 
    51     display(head);
    52 
    53     return 0;
    54 }

      上述两个代码都是暗含了链表的尾差法构建的链表,上边的是本人实现的方法,下边的是《C编程技巧117个问题解决方法示例》中的代码,明显后者更容易理解和操作,但是前者要简洁很多。各位同学可以据自己实际情况选择。

      

  • 相关阅读:
    51nod 1254 最大子段和 V2
    51nod 1115 最大M子段和 V3
    51nod 1053 最大M子段和 V2
    51nod 1052 最大M子段和
    51nod 1051 最大子矩阵和
    web.config或App.config中AttachDBFilenamex相对路径问题
    [转帖]unity3D OnTriggerEnter和OnCollisionEnter的一点个人心得(主要讲区别)
    unity3d 第一人称脚本解释MouseLook
    unity3d-游戏实战突出重围,整合游戏
    unity3d-游戏实战突出重围,第四天 添加角色
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/14046258.html
Copyright © 2020-2023  润新知