首页 > 其他分享 >C: Linked List

C: Linked List

时间:2023-11-13 19:37:52浏览次数:27  
标签:Family temp List pfirst brief pcurrent NULL Linked

 

/**
# 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

相关文章

  • 谈谈对ArrayList的理解以及扩容机制
     1.浅谈ArrayListArrayList类又称动态数组,同时实现了Collection和List接口,其内部数据结构由数组实现,因此可对容器内元素实现快速随机访问。但因为ArrayList中插入或删除一个元素需要移动其他元素,所以不适合在插入和删除操作频繁的场景下使用。ArrayList的容量可以随着元素的增......
  • 常见面试题-Redis底层的SDS、ZipList、ListPack
    Redis的SDS了解吗?答:Redis创建了SDS(simpledynamicstring)的抽象类型作为String的默认实现SDS的结构如下:structsdshdr{//字节数组,用于保存字符串charbuf[];//buf[]中已使用字节数量,称为SDS的长度intlen;//buf[]中尚未使用的字节数量intfree;}......
  • 无涯教程-Dart - Lists(列表)
    array是编程中非常常用的集合,Dart以List对象的形式表示数组,列表只是一组有序的象。dart:core库提供了List类,该类允许创建和操作列表。Dart中列表的逻辑表示如下-固定长度列表固定长度列表的长度不能在运行时更改,创建固定长度列表的语法如下:步骤1  - 声明列表下......
  • Java 集合—ArrayList
    ArrayList介绍ArrayList 的底层是数组队列,相当于动态数组。与Java中的数组相比,它的容量能动态增长。ArrayList类位于java.util包中,使用前需要引入它,语法格式如下:importjava.util.ArrayList;//引入ArrayList类ArrayList<E>objectName=newArrayList<>();//初始化......
  • cmake编译介绍--cmakelist.txt
    1.cmake编译简介 单个文件编译C/C++时:gccmain.c/g++main.cpp 多代码文件时:MakeFile,解决多文件编译难问题,运行make命令编译自动完成 cmake编译引入:根据一定的规则自动生成MakeFile的,也是有语法(cmake还是依赖make编译)。自动管理makefile文件,写起来也更方便、没有makefile......
  • java ArrayList的基本使用
    packagecom.elaina.test1;importjava.util.ArrayList;publicclasstest1{publicstaticvoidmain(String[]args){//1.创建集合的对象//泛型:限定集合中的存储数据的类型//ArrayList<String>list=newArrayList<String>();......
  • JavaSEday05 泛型,数据结构,List,Set集合
    javSEday05泛型,数据结构,List,Set今日目标泛型使用数据结构ListSet1泛型1.1泛型的介绍泛型是一种类型参数,专门用来保存类型用的最早接触泛型是在ArrayList,这个E就是所谓的泛型了。使用ArrayList时,只要给E指定某一个类型,里面所有用到泛型的地方都会被......
  • JavaSE day05【泛型,数据结构,List接口,Set接口】测评题
    选择题题目1(单选):查看下列代码,选出正确的传参()publicclassTest2{publicstaticvoidmain(String[]args){ArrayList<Integer>list1=newArrayList<Integer>();ArrayList<Number>list2=newArrayList<Number>();Arr......
  • 重新学习算法_Day3-哈希表&2283&str与list转换
    HashTable 感觉从原理上说会用但是实际应用感觉不知道有什么用或者不知道怎么用例如:给你一个下标从 0 开始长度为 n 的字符串 num ,它只包含数字。如果对于 每个 0<=i<n 的下标 i ,都满足数位 i 在 num 中出现了 num[i]次,那么请你返回 true ,否则返回......
  • STL容器list的模拟实现
    前言list是C++STL的容器之一,相比string和vector,list在插入和删除数据时的效率更高。因为list的存储模型是一个个节点,新增数据并不需要将旧空间销毁,重新开辟新空间。但是list访问数据的速度较差,因为需要从某个节点开始不断迭代一、关于list1.list的大小用不同的类型分别对库中的list......