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

C语言 百炼成钢23

时间:2016-06-19 12:48:20      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

/*
题目59:链表如下
typedef struct _LinkList
{
int data;
struct _LinkList*next;
} LinkList;
有如下结点数据域  1 2 3 4 5 6 7 8 12 19 。。。。
要求1:创建链表
要求2:删除结点值为偶数的结点 ;70分
要求3:编写测试用例  30分
*/


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct _linklist{
    int data;
    struct _linklist *pnext;
}LinkList;

//初始化链表头结点
int InitLink(LinkList **pout/*out*/){
    int ERRO_MSG = 0;
    if (pout==NULL)
    {
        ERRO_MSG = 1;
        printf("pout==NULL erro msg:%d\n", ERRO_MSG);
        return ERRO_MSG;
    }
    LinkList * phead = (LinkList *)malloc(sizeof(LinkList));
    if (phead==NULL)
    {
        ERRO_MSG = 2;
        printf("分配内存失败 erro msg:%d\n", ERRO_MSG);
        return ERRO_MSG;
    }
    phead->data = 0;
    phead->pnext = NULL;
    *pout = phead;
    return ERRO_MSG;
}

//创建链表节点
int CreateLink(LinkList *pin/*in*/,int data){
    int ERRO_MSG = 0;
    if (pin==NULL)
    {
        ERRO_MSG = 1;
        printf("pin==NULL erro msg:%d\n", ERRO_MSG);
        return ERRO_MSG;
    }
    LinkList *pHead = pin;
    LinkList *pCurrent = pHead->pnext;
    //创建新节点
    LinkList *pMalloc = (LinkList *)malloc(sizeof(LinkList));
    pMalloc->data = data;
    pMalloc->pnext = NULL;
    if (pCurrent==NULL)
    {
        pHead->pnext = pMalloc;
        return ERRO_MSG;
    }
    //遍历到链表节点末尾位置
    while (pCurrent->pnext){
        pCurrent = pCurrent->pnext;
    }
    pCurrent->pnext = pMalloc;
    return ERRO_MSG;
}

//删除结点值为偶数的结点
int DelLink(LinkList *pin/*in*/){
    int ERRO_MSG = 0;
    if (pin == NULL)
    {
        ERRO_MSG = 1;
        printf("pin==NULL erro msg:%d\n", ERRO_MSG);
        return ERRO_MSG;
    }
    LinkList *pHead = pin;
    LinkList *pCurrent = pHead->pnext;
    LinkList * pPrior = pHead;
    while (pCurrent){
        if (pCurrent->data%2==0)
        {
            pPrior->pnext = pCurrent->pnext;
            //释放这个节点
            free(pCurrent);
            pCurrent = NULL;
            pCurrent = pPrior->pnext;
        }
        else{
            pPrior = pCurrent;
            pCurrent = pCurrent->pnext;
        }
    }
    return ERRO_MSG;
}

//打印链表
void PrintfA(LinkList *pin/*in*/){
    if (pin==NULL)
    {
        printf("链表不可以为空!\n");
    }
    LinkList *pHead = pin;
    LinkList *pCurrent = pHead->pnext;
    while (pCurrent){
        printf("%d\n", pCurrent->data);
        pCurrent = pCurrent->pnext;
    }
}

//释放链表
int FreeLink(LinkList **pin/*in*/){
    int ERRO_MSG = 0;
    if (pin==NULL)
    {
        ERRO_MSG = 1;
        printf("pin==NULL erro msg:%d\n", ERRO_MSG);
        return ERRO_MSG;
    }
    LinkList *pHead = *pin;
    LinkList *pCurrent = pHead->pnext;
    LinkList *pPrior = pHead;
    while (pCurrent){
        pPrior->pnext = pCurrent->pnext;
        //释放内存
        if (pCurrent)
        {
            free(pCurrent);
            pCurrent = NULL;
        }
        pCurrent = pPrior->pnext;
    }
    if (pHead)
    {
        free(pHead);
        pHead = NULL;
    }
    return ERRO_MSG;
}

void main(){
    int i = 0,ret=0;
    //初始化链表头结点
    LinkList *pHead = NULL;
    //初始化头结点
    InitLink(&pHead);
    //创建节点
    for (i = 0; i < 9; i++)
    {
        ret=CreateLink(pHead, i + 1);
        if (ret!=0)
        {
            printf("创建链表节点报错!\n");
            //释放所有链表节点
            goto END;
        }
    }
    //打印所有节点
    PrintfA(pHead);
    //删除部分节点
    ret=DelLink(pHead);
    if (ret!=0)
    {
        printf("删除偶数节点报错!\n");
        //释放所有链表节点
        goto END;
    }
    //打印所有节点
    PrintfA(pHead);
END:
    //释放所有节点
    FreeLink(&pHead);
    system("pause");
}

 

技术分享

C语言 百炼成钢23

标签:

原文地址:http://www.cnblogs.com/zhanggaofeng/p/5597743.html

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