/** # 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
/** # 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; }
调用:、
displasyFamily();
输出:
标签:Family,temp,List,pfirst,brief,pcurrent,NULL,Linked From: https://www.cnblogs.com/geovindu/p/17829916.html