C: Linked List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | /** # encoding: utf-8 # 版权所有 2023 涂聚文有限公司 # 许可信息查看: # 描述: 嵌套结构体 # Author : geovindu,Geovin Du 涂聚文. # IDE : CLion 2023.1.1 c17 windows 10 # Datetime : 2023/11/13 17:35 # User : geovindu # Product : CLion # Project : ctest # File : Family.c # explain : 学习 */ // // Created by geovindu on 2023/11/13. // #ifndef CTEST_FAMILY_H #define CTEST_FAMILY_H #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <stdbool.h> /** * @brief 日期 */ typedef struct Date Date; /** * @brief 家庭 */ typedef struct Family Family; /** * @brief * */ struct Date { int day; //日 int month; //月 int year; //年 }; /** * @brief 家庭 * */ struct Family { Date dob; //日期 char name[20]; //姓名 char father[20]; //父亲 char mother[20]; //母亲 Family *next; // Pointer to next structure Family *previous; // Pointer to previous structure }; /** * @brief 获取输入的记录 * @return */ Family *getPerson( void ); /** * @brief 遍历输出 * @param forwards * @param pfirst * @param plast */ void showPeople( bool forwards, Family *pfirst, Family *plast); /** * @brief 释放内存 * @param pfirst */ void releaseMemory(Family *pfirst); /** * 显示 */ void displasyFamily(); #endif //CTEST_FAMILY_H |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | /** # encoding: utf-8 # 版权所有 2023 涂聚文有限公司 # 许可信息查看: # 描述: 嵌套结构体 # Author : geovindu,Geovin Du 涂聚文. # IDE : CLion 2023.1.1 c17 windows 10 # Datetime : 2023/11/13 17:35 # User : geovindu # Product : CLion # Project : ctest # File : Family.c # explain : 学习 */ // // Created by geovindu on 2023/11/13. // #include "../includes/Family.h" /** * @brief Function to input data on Family members * @return返回一个结构体 家庭记录 */ Family *getPerson( void ) { struct Family *temp = (Family*) malloc ( sizeof (Family)); // Define local pointer printf_s( "\nEnter the name of the person: " ); scanf_s( "%s" , temp->name, sizeof (temp->name)); printf_s( "\nEnter %s's date of birth (day month year); " , temp->name); scanf_s( "%d %d %d" , &temp->dob.day, &temp->dob.month, &temp->dob.year); printf_s( "\nWho is %s's father? " , temp->name); scanf_s( "%s" , temp->father, sizeof (temp->father)); printf_s( "\nWho is %s's mother? " , temp->name); scanf_s( "%s" , temp->mother, sizeof (temp->mother)); temp->next = temp->previous = NULL; // Set pointer members to NULL return temp; // Return address of Family structure } /********************************************************* *@brief 遍历所有记录 * * @param forwards * @param pfirst * @param plast * *********************************************************/ void showPeople( bool forwards, Family *pfirst, Family *plast) { printf_s( "\n" ); for (Family *pcurrent = forwards ? pfirst : plast ; pcurrent != NULL ; pcurrent = forwards ? pcurrent->next : pcurrent->previous) { printf_s( "%s was born %d/%d/%d and has %s and %s as parents.\n" , pcurrent->name, pcurrent->dob.day, pcurrent->dob.month, pcurrent->dob.year, pcurrent->father, pcurrent->mother); } } /** * @brief 释放内存 * @param pfirst */ void releaseMemory(Family *pfirst) { Family *pcurrent = pfirst; Family *temp = NULL; while (pcurrent) { temp = pcurrent; pcurrent = pcurrent->next; free (temp); } } /** * 显示 */ void displasyFamily() { Family *first = NULL; // Pointer to first person Family *current = NULL; // Pointer to current person Family *last = NULL; // Pointer to previous person char more = '\0' ; // Test value for ending input while ( true ) { printf_s( "\nDo you want to enter details of a%s person (Y or N)? " , first != NULL ? "nother" : "" ); scanf_s( " %c" , &more, sizeof (more)); if ( tolower (more) == 'n' ) break ; current = getPerson(); if (first == NULL) first = current; // Set pointer to first Family else { last->next = current; // Set next address for previous Family current->previous = last; // Set previous address for current } last = current; // Remember for next iteration } showPeople( true , first, last); // Tell them what we know releaseMemory(first); first = last = NULL; } |
调用:、
1 | displasyFamily(); |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | /** * @brief * * @param head_ref * @param new_data */ void push(Node ** head_ref, Node new_data) { struct Node* new_node = ( struct Node*) malloc ( sizeof ( struct Node)); new_node->id = new_data.id; new_node->name=new_data.name; new_node->next = (*head_ref); (*head_ref) = new_node; } /** * @brief * * @param p * @param q */ void merge(Node *p, Node **q) { Node *p_curr = p, *q_curr = *q; Node *p_next, *q_next; // While there are available // positions in p while (p_curr != NULL && q_curr != NULL) { // Save next pointers p_next = p_curr->next; q_next = q_curr->next; // Make q_curr as next of p_curr // Change next pointer of q_curr q_curr->next = p_next; // Change next pointer of p_curr p_curr->next = q_curr; // Update current pointers for // next iteration p_curr = p_next; q_curr = q_next; } // Update head pointer of second list *q = q_curr; } /** * @brief Get the Node object * * @return Node* */ Node *getNode( void ) { Node *temp = (Node*) malloc ( sizeof (Node)); printf_s( "\nEnter the node id: " ); scanf_s( " %d" , &temp->id); printf_s( "\nEnter the node name: " ); scanf_s( " %s" , &temp->name, sizeof (temp->name)); return temp; } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Cpp programming
标签:
结构体
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2022-11-13 CSharp: Bridge Pattern in donet 6
2010-11-13 css 自定义字体 Internet Explorer,Firefox,Opera,Safari