单链表例程


test_one_list.zip
/*开发环境 code::block //gdb gcc
 *代码已经通过调试
 *2013/7/23 15:53
 *lin大侠
 */宇宙和谐科技


/********************************************************************
 *Copyright (C), 2013-2014,维护宇宙和谐有限公司
 *FileName:
 *Author:                   Version:                Date:
 *LinPeng                   V1.01.01.001            2013.07.19
 *Description  :
 *Others:
 *Function List:
 *History      :
 *       1. Date        : 2013.07.19
 *          Author      : LinPeng
 *          Modification: Build.
 *
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Node.h"

int main()
{
    LINK_NODE* pLinkNode = NULL;
    int nodeValue = 100;

    /*creat a list*/
    pLinkNode = alloca_node(nodeValue);

    /*add one value in the list*/
    add_data(&pLinkNode, 200);

    add_data(&pLinkNode, 300);

    /*print informatiom*/
    print_node(pLinkNode);

    delete_data(&pLinkNode, 200);

    /*print informatiom*/
    print_node(pLinkNode);

    return 0;
}
//end of main

/********************************************************************
 *Copyright (C), 2013-2014,维护宇宙和谐有限公司
 *FileName:
 *Author:                   Version:                Date:
 *LinPeng                   V1.01.01.001            2013.07.19
 *Description  :
 *Others:
 *Function List:
 *History      :
 *       1. Date        : 2013.07.19
 *          Author      : LinPeng
 *          Modification: Build.
 *
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Node.h"
//(2)创建链表
LINK_NODE* alloca_node(int value)
{
    LINK_NODE* pLinkNode = NULL;
    pLinkNode = (LINK_NODE*)malloc(sizeof(LINK_NODE));

    pLinkNode->data = value;
    pLinkNode->next = NULL;
    return pLinkNode;
}

 //(3)删除链表
void delete_node(LINK_NODE** pNode)
{
    LINK_NODE** pNext;
    if(NULL == pNode || NULL == *pNode)
    {
        return ;
    }
    pNext = &(*pNode)->next;
    free(*pNode);
    delete_node(pNext);
}

// (4)链表插入数据
int _add_data(LINK_NODE** pNode, LINK_NODE* pDataNode)
{
    if(NULL == *pNode)
    {
        *pNode = pDataNode;
        return TRUE;
    }
    return _add_data(&(*pNode)->next, pDataNode);
}

int add_data(const LINK_NODE** pNode, int value)
{
    LINK_NODE* pDataNode;
    if(NULL == *pNode)
    {
        return FALSE;
    }
    pDataNode = alloca_node(value);
    assert(NULL != pDataNode);
    return _add_data((LINK_NODE**)pNode, pDataNode);
}

// (5)删除数据

int _delete_data(LINK_NODE** pNode, int value)
{
    LINK_NODE* pLinkNode;
    if(NULL == (*pNode)->next)
    {
        return FALSE;
    }

    pLinkNode = (*pNode)->next;
    if(value == pLinkNode->data)
    {
        (*pNode)->next = pLinkNode->next;
        free(pLinkNode);
        return TRUE;
    }
    else
    {
        return _delete_data(&(*pNode)->next, value);
    }
}

int delete_data(LINK_NODE** pNode, int value)
{
    LINK_NODE* pLinkNode;
    if(NULL == pNode || NULL == *pNode)
    {
        return FALSE;
    }

    if(value == (*pNode)->data)
    {
        pLinkNode = *pNode;
        *pNode = pLinkNode->next;
        free(pLinkNode);
        return TRUE;
    }

    return _delete_data(pNode, value);
}

// (6)查找数据
LINK_NODE* find_data(const LINK_NODE* pLinkNode, int value)
{
    if(NULL == pLinkNode)
    {
         return NULL;
    }

    if(value == pLinkNode->data)
    {
        return (LINK_NODE*)pLinkNode;
    }
        return find_data(pLinkNode->next, value);
}

// (7)打印数据
void print_node(const LINK_NODE* pLinkNode)
{
    if(pLinkNode)
    {
        printf("%d\n", pLinkNode->data);
        print_node(pLinkNode->next);
    }
}

// (8)统计数据
int count_node(const LINK_NODE* pLinkNode)
{
    if(NULL == pLinkNode)
    {
       return 0;
    }
    return 1 + count_node(pLinkNode->next);
}
//end of node.c

/********************************************************************
 *Copyright (C), 2013-2014,维护宇宙和谐有限公司
 *FileName:
 *Author:                   Version:                Date:
 *LinPeng                   V1.01.01.001            2013.07.19
 *Description  :
 *Others:
 *Function List:
 *History      :
 *       1. Date        : 2013.07.19
 *          Author      : LinPeng
 *          Modification: Build.
 *
*********************************************************************/
#ifndef __NODE_H__
#define __NODE_H__

#define TRUE     1
#define FALSE    0

typedef int BOOL;

//(1)结构体定义
typedef struct _LINK_NODE
{
    int data;
    struct _LINK_NODE* next;
}LINK_NODE;

LINK_NODE* alloca_node(int value);
void delete_node(LINK_NODE** pNode);
int _add_data(LINK_NODE** pNode, LINK_NODE* pDataNode);
int add_data(const LINK_NODE** pNode, int value);
int _delete_data(LINK_NODE** pNode, int value);
int delete_data(LINK_NODE** pNode, int value);
LINK_NODE* find_data(const LINK_NODE* pLinkNode, int value);
void print_node(const LINK_NODE* pLinkNode);
int count_node(const LINK_NODE* pLinkNode);

#endif /* __NODE_H__ */
//end of node.h



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值