/*开发环境 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