/*********************************************************************
Copyright (c) 2012 by silent_world, All rights reserved.
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
////////////////////////////////////////////////////////////////////
/*** 模块配置区 ***/
////////////////////////////////////////////////////////////////////
#define HAVE_OPEN_MODULE
#define HAVE_FULL_MODULE
#define HAVE_TEST_AGAIN
////////////////////////////////////////////////////////////////////
/*** 模块宏定义区 ***/
////////////////////////////////////////////////////////////////////
#define LINE_STRING_NULL (0)
#define LINE_STRING_SUCCESS (0)
#define LINE_STRING_FAIL (-1)
#define STR_BUF_LENGTH (128)
#define CLEAR_KEY do{fflush(stdin);}while(0);
////////////////////////////////////////////////////////////////////
/*** 函数声明区 ***/
////////////////////////////////////////////////////////////////////
void *line_string_create(char *buf);
void line_string_destroy(void *handle);
void *line_string_delete(void *handle, char cur_ch);
#ifdef HAVE_FULL_MODULE
/***测试是否存在内存泄漏***/
void *line_string_malloc(int size);
void line_string_free(void *handle);
void line_string_show(void *handle);
#endif
////////////////////////////////////////////////////////////////////
int main()
{
void *line_head = 0, *ret_head = 0;
char in_str[STR_BUF_LENGTH] = {0};
char delete_ch = 0;
#ifdef HAVE_TEST_AGAIN
int flag_ch = 0;
char in_tmp = 0;
#endif
#ifdef HAVE_TEST_AGAIN
while(1)
{
#endif
printf("Please input string:\n");
CLEAR_KEY
gets(in_str);
line_head = line_string_create(in_str);
if(LINE_STRING_NULL == line_head)
return LINE_STRING_FAIL;
#ifdef HAVE_FULL_MODULE
line_string_show(line_head);
#endif
CLEAR_KEY
scanf("%c", &delete_ch);
ret_head = line_string_delete(line_head, delete_ch);
if(LINE_STRING_NULL == ret_head)
{
return LINE_STRING_FAIL;
}
#ifdef HAVE_FULL_MODULE
line_string_show(ret_head);
#endif
line_string_destroy(ret_head);
#ifdef HAVE_TEST_AGAIN
printf("The Test is complete!\n");
printf("Press Enter key continue...\n");
CLEAR_KEY
scanf("%c", &in_tmp);
do{
flag_ch = 0;
system("cls");
printf("*************************\n");
printf("0. exit!\n");
printf("1. Help\n");
printf("2. Need to test again!\n");
printf("*************************\n");
CLEAR_KEY
scanf("%d", &flag_ch);
if(0 == flag_ch)
{
return LINE_STRING_SUCCESS;
}
else if(1 == flag_ch)
{
system("cls");
printf("Please set parameter correctlly!\n");
printf("HAVE_OPEN_MODULE
-- It's invalid, be add in next version!\n");
printf("HAVE_FULL_MODULE
-- use whole function!\n");
printf("HAVE_TEST_AGAIN
-- You can use the line_str mult!\n");
printf("Press Enter key continue...\n");
CLEAR_KEY
scanf("%c", &in_tmp);
}
else if(2 == flag_ch)
{
break;
}
else{
system("cls");
printf("Sorry, Error input, Please input again!\n");
printf("Press any key continue...\n");
CLEAR_KEY
scanf("%c", &in_tmp);
}
}while(1);
system("cls");
#endif
#ifdef HAVE_TEST_AGAIN
}
#endif
return LINE_STRING_SUCCESS;
}
/////////////////////////////////////////////////////////////////////
/*** 链表操作模块 ***/
/////////////////////////////////////////////////////////////////////
typedef struct _line_string_
{
char c_data;
struct _line_string_ *next;
}line_string;
/*******************************************************************
函数功能:基于输入字符串创建链表
输入参数:buf
需要添加到链表的字符串
输出参数:
LINE_STRING_NULL 创建失败,返回空指针
other 返回指向链表头指针
*******************************************************************/
void *line_string_create(char *buf)
{
line_string *line_head = 0, *pre_node = 0, *cur_node = 0;
char *data_p = buf;
int i = 0;
#ifdef HAVE_FULL_MODULE
#define malloc line_string_malloc
#endif
if((LINE_STRING_NULL == buf) || (0 == strlen(buf)))
return LINE_STRING_NULL;
line_head = (line_string *)malloc(sizeof(line_string));
if(LINE_STRING_NULL == line_head)
return LINE_STRING_NULL;
memset(line_head, 0, sizeof(line_string));
line_head->c_data = *data_p;
line_head->next = 0;
data_p++;
pre_node = line_head;
while(*data_p)
{
cur_node = (line_string *)malloc(sizeof(line_string));
if(LINE_STRING_NULL == cur_node)
return LINE_STRING_NULL;
memset(cur_node, 0, sizeof(line_string));
cur_node->c_data = *data_p;
cur_node->next = 0;
pre_node->next = cur_node;
pre_node = cur_node;
data_p++;
}
#ifdef HAVE_FULL_MODULE
#undef malloc
#endif
return line_head;
}
/*******************************************************************
函数功能: 删除链表
输入参数: handle
需要destroy的指针
输出参数: void
*******************************************************************/
void line_string_destroy(void *handle)
{
line_string *cur_node = (line_string *)handle;
line_string *next_node = 0;
#ifdef HAVE_FULL_MODULE
#define free line_string_free
#endif
while(cur_node)
{
next_node = cur_node->next;
free(cur_node);
cur_node = next_node;
}
#ifdef HAVE_FULL_MODULE
#undef free
#endif
}
#ifdef HAVE_FULL_MODULE
void *line_string_malloc(int size)
{
return malloc(size);
}
void line_string_free(void *handle)
{
free(handle);
}
#endif
/*******************************************************************
函数功能: 删除链表中结点
输入参数: handle
链表头
cur_ch
需要匹配的字符
输出参数: 链表头
*******************************************************************/
void *line_string_delete(void *handle, char cur_ch)
{
line_string *head_node = (line_string *)handle;
line_string *pre_node = 0, *cur_node = 0;
#ifdef HAVE_FULL_MODULE
#define free line_string_free
#endif
while((head_node) && (head_node->c_data == cur_ch))
{
cur_node = head_node->next;
free(head_node);
head_node = cur_node;
}
if(LINE_STRING_NULL == head_node)
{
return LINE_STRING_NULL;
}
pre_node = head_node;
cur_node = head_node->next;
while(cur_node)
{
if(cur_node->c_data == cur_ch)
{
pre_node->next = cur_node->next;
free(cur_node);
cur_node = pre_node->next;
}
else
{
pre_node = cur_node;
cur_node = cur_node->next;
}
}
#ifdef HAVE_FULL_MODULE
#undef free
#endif
return head_node;
}
#ifdef HAVE_FULL_MODULE
/*******************************************************************
函数功能: 打印链表信息
输入参数: handle
链表头
输出参数: 无
*******************************************************************/
void line_string_show(void *handle)
{
line_string *cur_node = (line_string *)handle;
while(cur_node)
{
printf("%c", cur_node->c_data);
cur_node = cur_node->next;
}
printf("\n");
}
#endif