#include "LinkedList.h"
int main(void)
{
LinkedList stList;
init_list(&stList);
insert_node(&stList, (datatype)100);
insert_node(&stList, (datatype)200);
insert_node(&stList, (datatype)300);
insert_node(&stList, (datatype)400);
insert_node(&stList, (datatype)500);
print_node(&stList);
remove_node(&stList, (datatype)300);
print_node(&stList);
return 0;
}
LinkedList.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned int uint32;
typedef int datatype;
typedef struct ListNode_S
{
datatype data;
struct ListNode_S *prior;
struct ListNode_S *next;
} ListNode;
typedef struct LinkedList_S
{
uint32 node_num;
ListNode *head;
} LinkedList;
int init_list(LinkedList * pstList);
int insert_node(LinkedList * pstList, datatype data);
int find_node(LinkedList * pstList, datatype data, ListNode * * ppstNode);
int remove_node(LinkedList * pstList, datatype data);
void print_node(LinkedList * pstList);
LinkedList.c
#include "LinkedList.h"
/*****************************************************************************
函 数 名 : init_list
功能描述 : 初始化链表
输入参数 : LinkedList *pstList
输出参数 : 无
返 回 值 : int
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2011年10月19日
作 者 : hemiao
修改内容 : 新生成函数
*****************************************************************************/
int init_list( LinkedList *pstList )
{
if (NULL == pstList)
{
return -1;
}
pstList->head = NULL;
pstList->node_num = 0;
return 0;
}
/*****************************************************************************
函 数 名 : insert_node
功能描述 : 向链表尾部插入节点
输入参数 : LinkedList *pstList
datatype data
输出参数 : 无
返 回 值 : int
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2011年10月19日
作 者 : hemiao
修改内容 : 新生成函数
*****************************************************************************/
int insert_node( LinkedList *pstList, datatype data )
{
ListNode *pstInsertNode = NULL;
ListNode *pstCurNode = NULL;
if (NULL == pstList)
{
return -1;
}
/* 创建一个节点 */
pstInsertNode = (ListNode *)malloc(sizeof(ListNode));
if (NULL == pstInsertNode)
{
printf("创建节点失败\n");
return -1;
}
memset(pstInsertNode, 0, sizeof(ListNode));
pstInsertNode->data = data;
pstCurNode = pstList->head;
if (NULL == pstCurNode)
{
pstList->head = pstInsertNode;
}
else
{
while (NULL != pstCurNode->next)
{
pstCurNode = pstCurNode->next;
}
pstCurNode->next = pstInsertNode;
pstInsertNode->prior = pstCurNode;
}
pstList->node_num++;
return 0;
}
/*****************************************************************************
函 数 名 : find_node
功能描述 : 查找链表中数据为data的节点
输入参数 : LinkedList *pstList
datatype data
ListNode **ppstNode
输出参数 : 无
返 回 值 : int
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2011年10月21日
作 者 : hemiao
修改内容 : 新生成函数
*****************************************************************************/
int find_node( LinkedList *pstList, datatype data, ListNode **ppstNode )
{
ListNode *pstCurNode = NULL;
if (NULL == pstList || NULL == ppstNode)
{
return -1;
}
pstCurNode = pstList->head;
while (NULL != pstCurNode)
{
if (pstCurNode->data == data)
{
*ppstNode = pstCurNode;
return 0;
}
pstCurNode = pstCurNode->next;
}
return -1;
}
/*****************************************************************************
函 数 名 : remove_node
功能描述 : 移除链表上的某个节点
输入参数 : LinkedList *pstList
datatype data
输出参数 : 无
返 回 值 : int
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2011年10月21日
作 者 : hemiao
修改内容 : 新生成函数
*****************************************************************************/
int remove_node( LinkedList *pstList, datatype data )
{
ListNode *pstRemoveNode = NULL;
int ret = -1;
if (NULL == pstList)
{
return -1;
}
ret = find_node(pstList, data, &pstRemoveNode);
if (-1 == ret)
{
printf("要删除的节点不存在\n");
return 0;
}
/* 消除指针联系 */
pstRemoveNode->prior->next = pstRemoveNode->next;
if (NULL != pstRemoveNode->next)
{
pstRemoveNode->next->prior = pstRemoveNode->prior;
}
/* 释放节点 */
free(pstRemoveNode);
pstRemoveNode = NULL;
return 0;
}
/*****************************************************************************
函 数 名 : print_node
功能描述 : 打印节点内容
输入参数 : LinkedList *pstList
输出参数 : 无
返 回 值 : void
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2011年10月21日
作 者 : hemiao
修改内容 : 新生成函数
*****************************************************************************/
void print_node( LinkedList *pstList )
{
ListNode *pstCurNode = NULL;
if (NULL == pstList)
{
return;
}
pstCurNode = pstList->head;
printf("List start:#######################\n");
while (NULL != pstCurNode)
{
printf("%d\n", (int)pstCurNode->data);
pstCurNode = pstCurNode->next;
}
printf("List end:#########################\n");
return;
}