码迷,mamicode.com
首页 > 编程语言 > 详细

C语言链表

时间:2017-07-08 21:52:13      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:secure   logs   err   typedef   lease   system   没有   res   oid   

#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("\nnot found age=%d\n", 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);

    //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");

}

 

C语言链表

标签:secure   logs   err   typedef   lease   system   没有   res   oid   

原文地址:http://www.cnblogs.com/nfcm/p/7138008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!