main.c
******************************************************************************
文 件 名 : main.c
版 本 号 : 初稿
作 者 : wangjinchao 00218970
生成日期 : 2012年6月30日
最近修改 :
功能描述 : double_link_s 测试函数
函数列表 :
修改历史 :
1.日 期 : 2012年6月30日
作 者 : wangjinchao 00218970
修改内容 : 创建文件
******************************************************************************/
/*----------------------------------------------*
* 外部变量说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 外部函数原型说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 内部函数原型说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 全局变量 *
*----------------------------------------------*/
/*----------------------------------------------*
* 模块级变量 *
*----------------------------------------------*/
/*----------------------------------------------*
* 常量定义 *
*----------------------------------------------*/
/*----------------------------------------------*
* 宏定义 *
*----------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include "double_link.h"
static void insert_and_print(double_link_s *head, int value);
static void delete_and_print(double_link_s *head, int value);
int main(void)
{
double_link_s *head = NULL;
head = (double_link_s *) malloc(sizeof(double_link_s));
if (head == NULL)
{
printf("内存不足!\n");
exit(0);
}
printf("Welcome!\n\n");
init_head(head);
printf("emptyList:");
print_nodes(head);
insert_and_print(head, 1);
insert_and_print(head, 2);
insert_and_print(head, 3);
insert_and_print(head, 2);
delete_and_print(head, 2);
delete_and_print(head, 4);
free(head);
head = NULL;
printf("\nThank you! Goodbye!\n");
getchar();
return 0;
}
/*****************************************************************************
函 数 名 : insert_and_print
功能描述 : 插入并打印相关信息
输入参数 : double_link_s *head
int value
输出参数 : 无
返 回 值 : static void
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2012年6月30日
作 者 : wangjinchao 00218970
修改内容 : 新生成函数
*****************************************************************************/
static void insert_and_print(double_link_s *head, int value)
{
printf("insert%3d:", value);
insert_node(head, value);
print_nodes(head);
}
/*****************************************************************************
函 数 名 : delete_and_print
功能描述 : 删除并打印相关信息
输入参数 : double_link_s *head
int value
输出参数 : 无
返 回 值 : static void
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2012年6月30日
作 者 : wangjinchao 00218970
修改内容 : 新生成函数
*****************************************************************************/
static void delete_and_print(double_link_s *head, int value)
{
printf("delete%3d:", value);
delete_node(head, value);
print_nodes(head);
}
double_link.h
生成日期 : 2012年6月29日
最近修改 :
功能描述 : 有序双向链表的实现,可以初始化表头,添加,删除
函数列表 :
修改历史 :
1.日 期 : 2012年6月29日
作 者 : wangjinchao 00218970
修改内容 : 创建文件
******************************************************************************/
/*----------------------------------------------*
* 外部变量说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 外部函数原型说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 内部函数原型说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 全局变量 *
*----------------------------------------------*/
/*----------------------------------------------*
* 模块级变量 *
*----------------------------------------------*/
/*----------------------------------------------*
* 常量定义 *
*----------------------------------------------*/
/*----------------------------------------------*
* 宏定义 *
*----------------------------------------------*/
//双向链表插入删除代码编写
#ifndef __DOUBLELINK_H__
#define __DOUBLE_LINK_H__
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
/******************************************************************************
结构名称: double_link_s
基本功能: 双向链表节点结构体,用于保存前后节点的指针和当前节点的Value值
特殊处理: 若value为-1, 表示该节点为链表头
修改历史:
1.日 期: 2012年6月29日
2.姓 名: wangjinchao 00218970
3.修改内容: 添加注释
******************************************************************************/
typedef struct double_link_struct
{
int value;
struct double_link_struct *pre;
struct double_link_struct *next;
} double_link_s;
/*****************************************************************************
函 数 名 : init_head
功能描述 : 初始化链表头, 需要给表头预分配空间
输入参数 : double_link_s *head
输出参数 : 无
返 回 值 : int
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2012年6月30日
作 者 : wangjinchao 00218970
修改内容 : 新生成函数
*****************************************************************************/
int init_head(double_link_s *head);
/*****************************************************************************
函 数 名 : insert_node
功能描述 : 插入节点, 若表头未初始化, 程序将返回错误值, 否则将新节点按照逆
序插入链表
输入参数 : double_link_s *head
int value
输出参数 : 无
返 回 值 : int
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2012年6月30日
作 者 : wangjinchao 00218970
修改内容 : 新生成函数
*****************************************************************************/
int insert_node(double_link_s *head, int value);
/*****************************************************************************
函 数 名 : delete_node
功能描述 : 删除节点, 将链表内所有节点value等于输入参数的节点删除, 删除后
刷新前后向指针
输入参数 : double_link_s *head
int value
输出参数 : 无
返 回 值 : int
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2012年6月30日
作 者 : wangjinchao 00218970
修改内容 : 新生成函数
*****************************************************************************/
int delete_node(double_link_s *head, int value);
/*****************************************************************************
函 数 名 : print_nodes
功能描述 : 按照指定格式打印链表内容
输入参数 : const double_link_s *head
输出参数 : 无
返 回 值 : int
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2012年6月30日
作 者 : wangjinchao 00218970
修改内容 : 新生成函数
*****************************************************************************/
int print_nodes(const double_link_s *head);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* __DOUBLELINK_H__ */
double_link.c
生成日期 : 2012年6月30日
最近修改 :
功能描述 : double_link_s相关功能实现文件, 主要有:初始化, 添加, 删除, 打印
函数列表 :
delete_node
init_head
insert_node
print_nodes
修改历史 :
1.日 期 : 2012年6月30日
作 者 : wangjinchao 00218970
修改内容 : 创建文件
******************************************************************************/
/*----------------------------------------------*
* 外部变量说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 外部函数原型说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 内部函数原型说明 *
*----------------------------------------------*/
/*----------------------------------------------*
* 全局变量 *
*----------------------------------------------*/
/*----------------------------------------------*
* 模块级变量 *
*----------------------------------------------*/
/*----------------------------------------------*
* 常量定义 *
*----------------------------------------------*/
/*----------------------------------------------*
* 宏定义 *
*----------------------------------------------*/
#include "double_link.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*****************************************************************************
函 数 名 : init_link_head
功能描述 : 链表表头初始化,为表头分配空间,并初始化表头内指针为NULL
输入参数 : double_link_s * head
输出参数 : 无
返 回 值 : int
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2012年6月29日
作 者 : wangjinchao 00218970
修改内容 : 新生成函数
*****************************************************************************/
int init_head(double_link_s *head)
{
if (head == NULL)
{
return -1;
}
memset(head, 0, sizeof(double_link_s));
head->value = -1;
return 0;
}
/*****************************************************************************
函 数 名 : insert_node
功能描述 : 为链表插入节点,节点值等于value,插入时自动排序
输入参数 : double_link_s *head
int value
输出参数 : 无
返 回 值 : int
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2012年6月29日
作 者 : wangjinchao 00218970
修改内容 : 新生成函数
*****************************************************************************/
int insert_node(double_link_s *head, int value)
{
double_link_s *new_node = NULL;
double_link_s *current = NULL;
current = head;
if (head == NULL)
{
return -1;
}
new_node = (double_link_s *) malloc(sizeof(double_link_s));
if (new_node == NULL)
{
return -2;
}
memset(new_node, 0, sizeof(double_link_s));
new_node->value = value;
if (current == NULL)
{
head->next = new_node;
new_node->pre = head;
return 0;
}
while (current != NULL)
{
if (current->value <= value && current->next != NULL)
{
current = current->next;
}
else
{
break;
}
}
if (current->value > value) //未达到链表结尾
{
new_node->pre = current->pre;
current->pre->next = new_node;
new_node->next = current;
current->pre = new_node;
}
else //链表末尾
{
current->next = new_node;
new_node->pre = current;
new_node->next = NULL;
}
return 0;
}
/*****************************************************************************
函 数 名 : delete_node
功能描述 : 删除双向链表中value等于输入参数的节点,删除时检查是否有重复项,
有的话都删除
输入参数 : double_link_s *head
int value
输出参数 : 无
返 回 值 : int
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2012年6月29日
作 者 : wangjinchao 00218970
修改内容 : 新生成函数
*****************************************************************************/
int delete_node(double_link_s *head, int value)
{
double_link_s *current = head->next;
double_link_s *node_before_delete = NULL;
if (head == NULL)
{
return -1;
}
while (current != NULL)
{
if (current->value != value)
{
current = current->next;
}
else
{
break;
}
}
if (current == NULL) // 没找到 直接退出
{
printf(" no node with value of %d! \n ", value);
return -2;
}
else //current 指向待删除的节点
{
node_before_delete = current->pre;
do
{
current = current->next;
free(current->pre);
}
while ((current != NULL) && (current->value == value));
node_before_delete->next = current;
if (current != NULL)//删除后还有其他节点, 需要刷新前向指针
{
current->pre = node_before_delete;
}
return 0;
}
}
/*****************************************************************************
函 数 名 : print_nodes
功能描述 : 按照指定格式遍历并打印链表的所有值
输入参数 : double_link_s *head
输出参数 : 无
返 回 值 : int
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2012年6月29日
作 者 : wangjinchao 00218970
修改内容 : 新生成函数
*****************************************************************************/
int print_nodes(const double_link_s *head)
{
double_link_s *current = NULL;
int count = 0;
if (head == NULL)
{
return -1;
}
printf(" begin<-->");
current = head->next;
while (current != NULL)
{
count++;
if (count%8 == 0)
{
printf("\n");
}
printf("%3d <-->", current->value);
current = current->next;
}
printf("end\n");
return 0;
}