C: Linked List

 

/**
# 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();

  

输出:

 

 

/**
 * @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;
}

  

 

posted @ 2023-11-13 19:15  ®Geovin Du Dream Park™  阅读(13)  评论(0)    收藏  举报