栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

malloc一个动态链表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

malloc一个动态链表

描述:按图实现一个人员管理功能(录入,增加,删除)

思考:是否可以将 动态链表的实现,封装成一套宏接口?(实现后更新)

代码实现:

1. 变量声明 和 main函数:

#include 
#include 
#include 

int listCount = 0; 
typedef struct _Student
{
    char name[20];
    int  number;

    struct _Student *pNext;
}STUDENT;

void Print_Student_Info(STUDENT *pList)
{
    STUDENT *pNewNode = NULL;

    unsigned int index = 0;

    if (NULL == pList ||
        NULL == pList->pNext)
    {
        printf("n---- student info is not exist ----nn");
        return ;
    }

    printf("n---- the list has %d members ----nn", listCount);
    
    index = 1;
    pNewNode = pList->pNext;
    while (NULL != pNewNode)
    {
        printf("the NO%02d member is:n", index);
        printf("    the name is %sn", pNewNode->name);
        printf("    the number is %dnn", pNewNode->number);

        index++;
        pNewNode = pNewNode->pNext;
    }
    
    return ;
}

int main(int argc, char *argv[])
{
    STUDENT *pStudentlist = NULL;

    
    pStudentlist = Create_Student_Info();
    Print_Student_Info(pStudentlist);
    if (NULL == pStudentlist)
    {
        return 0;
    }

    
    Insert_Head_Student_Info(pStudentlist);
    Print_Student_Info(pStudentlist);

    
    Insert_End_Student_Info(pStudentlist);
    Print_Student_Info(pStudentlist);

    
    Delete_Student_Info(pStudentlist);
    Print_Student_Info(pStudentlist);
    return 0;
}

2. 录入数据功能:

 

实现:(退出时会创建一个结点,需要free,否则会造成内存泄漏)

STUDENT *Create_Student_Info()
{
    STUDENT *pHeadNode = NULL;
    STUDENT *pEndNode  = NULL;
    STUDENT *pNewNode  = NULL; 

    
    printf("please enter [Name Number]:n");
    pNewNode = (STUDENT *)malloc(sizeof(STUDENT));
    if (NULL == pNewNode)
    {
        printf("init head node failed!n");
        return NULL;
    }
    memset(pNewNode, 0, sizeof(STUDENT));

    listCount = 0;
    pHeadNode = pNewNode;
    pEndNode  = pNewNode;

    do{
        
        pNewNode = (STUDENT *)malloc(sizeof(STUDENT));
        if (NULL == pNewNode)
        {
            printf("create %d-th node failed!n", (listCount+1));
            return pHeadNode;
        }
        scanf("%s", &pNewNode->name[0]);
        scanf("%d", &pNewNode->number);
        pNewNode->pNext = NULL;

        if (0 != pNewNode->number)
        {
            listCount++;
            pEndNode->pNext = pNewNode;
            pEndNode        = pNewNode;
        }

    }while (0 != pNewNode->number);
    
    
    free((void*)pNewNode);

    return pHeadNode;
}

3. 插入数据功能

3.1 头插:

实现:

void Insert_Head_Student_Info(STUDENT *pList)
{
    STUDENT *pNewNode = NULL;

    if (NULL == pList)
    {
        printf("n---- student is not exist, insert head failed! ----nn");
        return ;
    }

    printf("insert member at head:n");
    pNewNode = (STUDENT *)malloc(sizeof(STUDENT));
    if (NULL == pNewNode)
    {
        printf("ncreate insert node at head failed!n");
        return ;
    }
    scanf("%s", &pNewNode->name[0]);
    scanf("%d", &pNewNode->number);
    pNewNode->pNext = NULL;

    
    listCount++;
    pNewNode->pNext = pList->pNext;
    pList->pNext    = pNewNode;

    return ;
}

3.2 尾插:

 实现:

void Insert_End_Student_Info(STUDENT *pList)
{
    STUDENT *pEndNode = NULL;
    STUDENT *pNewNode = NULL;

    if (NULL == pList)
    {
        printf("ninsert member node at end failed!n");
        return ;
    }

    pEndNode = pList;
    while(NULL != pEndNode->pNext)
    {
        pEndNode = pEndNode->pNext;
    }

    printf("insert member at end:n");
    pNewNode = (STUDENT *)malloc(sizeof(STUDENT));
    if (NULL == pNewNode)
    {
        printf("ncreate insert node at end failed!n");
        return ;
    }
    scanf("%s", &pNewNode->name[0]);
    scanf("%d", &pNewNode->number);
    pNewNode->pNext = NULL;
    
    
    listCount++;
    pEndNode->pNext = pNewNode;

    return ;
}

4. 删除数据功能:

实现:

void Delete_Student_Info(STUDENT *pList)
{
    STUDENT *pPerNode = NULL;
    STUDENT *pCurNode = NULL;

    char deleteName[20];

    if (NULL == pList ||
        NULL == pList->pNext)
    {
        printf("n---- student info is not exist ----nn");
        return ;
    }

    printf("delete, please input student name:");
    scanf("%s", &deleteName[0]);

    pPerNode = pList;
    pCurNode = pList->pNext;
    while (NULL != pCurNode)
    {
        if (0 == strcmp(pCurNode->name, deleteName))
        {
            break;
        }

        pPerNode = pCurNode;
        pCurNode = pCurNode->pNext;
    }
    
    if (NULL != pCurNode)
    {
        printf("%s student delete successn", deleteName);
        pPerNode->pNext = pCurNode->pNext;
        free(pCurNode);
        listCount--;
    }
    else
    {
        printf("%s student not exist!n", deleteName);
    }

    return ;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/780253.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号