| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 744 人关注过本帖
标题:采用链式存储结构存储中,输入一个字符串r,删除串r中所有值等于ch的字符。
只看楼主 加入收藏
zx2612804
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-5-18
结帖率:0
收藏
已结贴  问题点数:20 回复次数:6 
采用链式存储结构存储中,输入一个字符串r,删除串r中所有值等于ch的字符。
急求解答
搜索更多相关主题的帖子: 存储 字符串 
2012-05-18 10:50
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:20 
你已经说的非常清楚了。
采用链式存储结构存储,输入一个字符串r,删除r中所有值等于ch的字符
就是这个流程。
不知道你开发过程中碰到什么问题了?
2012-05-18 11:11
zx2612804
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-5-18
收藏
得分:0 
回复 2楼 silent_world
我就不会做 您能给做下完整的程序么
2012-05-18 12:42
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:0 
没时间写完整代码,可以给你一个思路。
1、建立struct
    typedef struct _LINE_STR_DATA_
   {
        char cur_ch;
        struct _LINE_STR_DATA_ *next;
   }LINE_STR_DATA;

2、建立链表,且输入字符串;
3、删除链表;

最好能把逻辑控制与实现分开,如果要做完美一点,就模块化,留下合适的接口,全部封闭即可。
你可以写一个代码,再慢慢完善,过程非常重要。

2012-05-19 09:28
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:0 
稍等一会,今天兴致高,写个代码你拿去玩吧
2012-05-19 09:32
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:0 
/*********************************************************************
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


2012-05-19 11:36
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:0 
这个代码已经初步测试完成
你可以多方面测试测试,应该能满足你的要求。
呵呵,记得给分啊——
2012-05-19 11:37
快速回复:采用链式存储结构存储中,输入一个字符串r,删除串r中所有值等于ch的字 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015351 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved