• C语言链表实现


    #define _CRT_SECURE_NO_WARNINGS
    #include "stdio.h"
    #include "stdlib.h"
    
    typedef struct _Teacher
    {
        int age;
        struct _Teacher *next;
    }Teacher;
    
    Teacher* create_teacher();
    int print_teacher(Teacher * pHeader);
    int add_teacher(Teacher * pHeader, int x, int y);
    int del_teacher(Teacher *pHeader, int x);
    int reverse_teacher(Teacher *pHeader);
    int free_teacher(Teacher *pHeader);
    
    
    
    //创建链表
    Teacher* create_teacher()
    {
        Teacher* pHeader = (Teacher*)malloc(sizeof(Teacher));
        if (pHeader == NULL)
        {
            printf("err:malloc pheader err");
            return NULL;
        }
        Teacher* pCur = NULL;
        pCur = pHeader;
    
        pHeader->age = 0;
        pHeader->next = NULL;
        int i = 0;
        while (1)
        {
            printf("please enter your age:");
            scanf("%d", &i);
            if (i == -1)
            {
                break;
            }
            Teacher* temp = (Teacher*)malloc(sizeof(Teacher));
            if (temp == NULL)
            {
                free_teacher(pHeader);
                return -1;
            }
            temp->age = i;
            temp->next = NULL;
    
            pCur->next = temp;
            pCur = temp;
    
        }
        return pHeader;
    }
    //读取链表
    int print_teacher(Teacher * pHeader)
    {
        if (pHeader == NULL)
        {
            printf("err print_teacher:pHeader=NULL");
            return -1;
        }
        Teacher * pCur = pHeader->next;
    
        while (pCur)
        {
            printf("%d ", pCur->age);
            pCur = pCur->next;
        }
    
    }
    //在age=x前面插入y,没有则插入到最后
    int add_teacher(Teacher * pHeader, int x, int y)
    {
        if (pHeader == NULL)
        {
            printf("err add_teacher:pHeader=NULL");
            return -1;
        }
        Teacher * pM = (Teacher *)malloc(sizeof(Teacher));
        if (pM == NULL)
        {
            free_teacher(pHeader);
            return -1;
        }
        pM->age = y;
        Teacher *pPre = NULL, *pCur = NULL;
        pPre = pHeader;
        pCur = pPre->next;
    
        while (pCur != NULL)
        {
            if (pCur->age == x)
            {
                break;
            }
    
            pPre = pCur;
            pCur = pCur->next;
    
        }
        pM->next = pCur;
        pPre->next = pM;
    
    
    }
    //删除链表节点
    int del_teacher(Teacher *pHeader, int x)
    {
        if (pHeader == NULL)
        {
            printf("err del_teacher:pheader==NULL");
            return -1;
    
        }
        Teacher *delT = NULL;
        Teacher *pPre = pHeader;
        Teacher *pCur = pHeader->next;
        while (pCur != NULL)
        {
            if (pCur->age == x)
            {
                delT = pCur;
                break;
            }
            pPre = pCur;
            pCur = pCur->next;
    
        }
        if (delT == NULL)
        {
            printf("
    not found age=%d
    ", x);
            return 0;
        }
        pPre->next = pCur->next;
        free(delT);
        return 0;
    
    }
    //释放内存
    int free_teacher(Teacher *pHeader)
    {
        Teacher *pCur = pHeader;
        Teacher *next = pCur->next;
        while (pCur != NULL)
        {
            next = pCur->next;
            free(pCur);
            pCur = next;
        }
        return 0;
    }
    //逆向链表
    int reverse_teacher(Teacher *pHeader)
    {
        if (pHeader == NULL)
        {
            printf("err del_teacher:pheader==NULL");
            return -1;
    
        }
        if (pHeader->next == NULL || pHeader->next->next == NULL)
        {
            return 0;
        }
        Teacher * pPre = pHeader->next, *pCur = pHeader->next->next,*temp =NULL;
        pPre->next = NULL;
        while (pCur != NULL)
        {
            temp = pCur->next;
    
            pCur->next = pPre;
    
            pPre = pCur;
    
            pCur = temp;
    
        }
    
        pHeader->next = pPre;
        return 0;
    
    }
    void main()
    {
        Teacher * pHeader = NULL;
        pHeader = create_teacher();
        int res = 0;
        if (pHeader == NULL)
        {
            printf("err create_teacher");
            return;
    
        }
        res = print_teacher(pHeader);
       if(res==-1)
       {
         printf("err print_teacher");
            return;

    }
    //res = add_teacher(pHeader, 20, 19); //res = print_teacher(pHeader); //res = del_teacher(pHeader, 20); //res = print_teacher(pHeader); res =reverse_teacher(pHeader); res = print_teacher(pHeader); res = free_teacher(pHeader); system("pause"); }
  • 相关阅读:
    使用IDEA创建SpringMVC项目
    Spring基于注解的配置——@Required、@Autowired、@Qualifier示例及与传统注入方法的对比
    Spring基于构造函数和设值函数的依赖注入
    Mysql——通配符和正则表达式的使用
    Spring Bean的生命周期、后置处理器、定义继承
    Spring Bean的定义及作用域
    MySql——使用where子句过滤数据
    MySql——创建数据表,查询数据,排序查询数据
    MySQL的安装+可视化工具+JDBC的增删改查
    slave_master_info和slave_relay_log_info中的Master_log_pos不一致
  • 原文地址:https://www.cnblogs.com/nfcm/p/7138008.html
Copyright © 2020-2023  润新知