| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 368 人关注过本帖
标题:顺序表中查找问题
只看楼主 加入收藏
qazkangyi
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-5-23
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
顺序表中查找问题
创建一个顺序表,输出该顺序表,然后在顺序表中查找元素X,若找到,输出X所在的序号,否则输出“无该元素”
搜索更多相关主题的帖子: 元素 
2012-05-23 17:34
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:20 
/*********************************************************************
Copyright (c) 2012 by silent_world, All rights reserved.
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


////////////////////////////////////////////////////////////////////
/*** 模块配置区 ***/
////////////////////////////////////////////////////////////////////
#define HAVE_SUPPORT_HEAP
#define HAVE_FULL_MODULE
#define HAVE_TEST_AGAIN

////////////////////////////////////////////////////////////////////
/*** 模块宏定义区 ***/
////////////////////////////////////////////////////////////////////
#define TABLE_STRING_NULL (0)
#define TABLE_STRING_SUCCESS (0)
#define TABLE_STRING_FAIL (-1)
#define STR_BUF_LENGTH (128)
#define TALBE_LENGTH (128)
#define CLEAR_KEY do{fflush(stdin);}while(0);

#define PRINTF_MENU system("cls"); \
                    printf("*************************\n"); \
                    printf("1. Help\n"); \
                    printf("2. Need to search again!\n"); \
                    printf("3. Need to test again!\n"); \
                    printf("4. exit!\n"); \
                    printf("*************************\n");

////////////////////////////////////////////////////////////////////
/*** 函数声明区 ***/
////////////////////////////////////////////////////////////////////

void *table_string_create();
void table_string_destroy(void *handle);
int table_string_add(void *handle, char *cur_str);
void table_string_show(void *handle);
int table_string_search(void *handle, char *cur_str);
#ifdef HAVE_FULL_MODULE
/***测试是否存在内存泄漏***/
void *table_string_malloc(int size);
void table_string_free(void *handle);
#endif

////////////////////////////////////////////////////////////////////

int main()
{
     void *table_head = 0;
     char in_str[STR_BUF_LENGTH] = {0};
     int ret = -1;
#ifdef HAVE_TEST_AGAIN
     int flag_ch = 0;
     char in_tmp = 0;
#endif

#ifdef HAVE_TEST_AGAIN
     while(1)
     {
#endif
          table_head = table_string_create();
          if(TABLE_STRING_NULL == table_head)
               return TABLE_STRING_FAIL;

          printf("Please input string, 输入0结束:\n");
          while(1)
          {
               CLEAR_KEY
               gets(in_str);
               if(0 == strcmp(in_str, "0"))
                    break;
               ret = table_string_add(table_head, in_str);
               if(TABLE_STRING_FAIL == ret)
                    break;
          }
 
#ifdef HAVE_FULL_MODULE
          table_string_show(table_head);
#endif

          printf("Please input search string:\n");
          CLEAR_KEY
          gets(in_str);

          ret = table_string_search(table_head, in_str);
          if(TABLE_STRING_FAIL == ret)
          {
               printf("Don't find!\n");
          }
          else
               printf("The number is %d\n", ret);

#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;
               PRINTF_MENU
                    
               CLEAR_KEY
               scanf("%d", &flag_ch);
               
               if(1 == flag_ch)
               {
                    system("cls");
                    printf("Please set parameter correctlly!\n");
                    printf("HAVE_SUPPORT_HEAP  -- user heap buffer!\n");
                    printf("HAVE_FULL_MODULE   -- use whole function!\n");
                    printf("HAVE_TEST_AGAIN    -- You can use the table_str mult!\n");
                    
                    printf("Press Enter key continue...\n");
                    CLEAR_KEY
                    scanf("%c", &in_tmp);
               }
               else if(2 == flag_ch)
               {
                    system("cls");
                    printf("Please input search string:\n");
                    CLEAR_KEY
                    gets(in_str);
                    
                    ret = table_string_search(table_head, in_str);
                    if(TABLE_STRING_FAIL == ret)
                    {
                         printf("Don't find!\n");
                    }
                    else
                         printf("The number is %d\n", ret);

                    printf("Press Enter key continue...\n");
                    CLEAR_KEY
                    scanf("%c", &in_tmp);
                    
               }
               else if(3 == flag_ch)
               {
                    break;
               }
               else if(4 == flag_ch)
               {
                    table_string_destroy(table_head);
                    return TABLE_STRING_SUCCESS;
               }
               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);
#endif

          table_string_destroy(table_head);


          system("cls");

#ifdef HAVE_TEST_AGAIN
     }
#endif

     return TABLE_STRING_SUCCESS;
}



/////////////////////////////////////////////////////////////////////
/*** 顺序表操作模块 ***/
/////////////////////////////////////////////////////////////////////
typedef struct _table_item_
{
     char c_data[STR_BUF_LENGTH];
     int flag;
     int status;
}table_item;

typedef struct _talbe_example_
{
     table_item table_data[TALBE_LENGTH];
}talbe_example;

enum{
     TALBE_NULL,
     TABLE_USING
};

////////////////////////////////////////////////////////////////////
/*** 全局变量区 ***/
////////////////////////////////////////////////////////////////////
#if !define(HAVE_SUPPORT_HEAP)
table_item table_data[TALBE_LENGTH] = {0};
#endif

#ifdef HAVE_FULL_MODULE
#define malloc table_string_malloc
#define free table_string_free
#endif
/*******************************************************************
函数功能:创建顺序表
输入参数:void
输出参数:
          TABLE_STRING_NULL 创建失败,返回空指针
          other 返回指向链表头指针
*******************************************************************/
void *table_string_create()
{
#ifdef HAVE_SUPPORT_HEAP   
     talbe_example *me = 0;
     int i = 0;

     me = (talbe_example *)malloc(sizeof(talbe_example));
     if(TABLE_STRING_NULL == me)
          return TABLE_STRING_NULL;

     memset(me, 0, sizeof(talbe_example));

     return me;
#else
     return &table_data[0];
#endif
     
}


/*******************************************************************
函数功能: 销毁队列
输入参数: handle  队列指针
输出参数: void
*******************************************************************/
void table_string_destroy(void *handle)
{
#ifdef HAVE_SUPPORT_HEAP   
     talbe_example *me = (talbe_example *)handle;

     if(TABLE_STRING_NULL != me)
          free(me);
#else
     memset(handle, 0, sizeof(table_item) * TALBE_LENGTH);
#endif
}

/*******************************************************************
函数功能: 添加一个结点
输入参数: handle  队列指针
           cur_str  需要添加的字符
输出参数: TABLE_STRING_SUCCESS 添加成功
           TABLE_STRING_FAIL  添加失败
*******************************************************************/
int table_string_add(void *handle, char *cur_str)
{
     int i = 0;
#ifdef HAVE_SUPPORT_HEAP
     talbe_example *head_node = (talbe_example *)handle;
     table_item *me = 0;

     if(TABLE_STRING_NULL != head_node)
          me = head_node->table_data;
#else
     table_item *me = (table_item *)handle;
#endif


     for(i = 0; i < TALBE_LENGTH; i++)
     {
          if(me[i].status == TALBE_NULL)
          {
               me[i].status = TABLE_USING;
               strcpy(me[i].c_data, cur_str);

               return TABLE_STRING_SUCCESS;
          }
          else if(0 == strcmp(me[i].c_data, cur_str))
               break;
     }

     return TABLE_STRING_FAIL;
}

/*******************************************************************
函数功能: 查找某个节点
输入参数: handle  队列指针
           cur_str  需要添加的字符
输出参数: TABLE_STRING_FAIL 查找失败
           other 序列号
*******************************************************************/
int table_string_search(void *handle, char *cur_str)
{
     int i = 0;
#ifdef HAVE_SUPPORT_HEAP
     talbe_example *head_node = (talbe_example *)handle;
     table_item *me = 0;
     
     if(TABLE_STRING_NULL != head_node)
          me = head_node->table_data;
#else
     table_item *me = (table_item *)handle;
#endif

     for(i = 0; i < TALBE_LENGTH; i++)
     {
          if(me[i].status == TABLE_USING)
          {
               if(0 == strcmp(me[i].c_data, cur_str))
                    return i;
          }
          else
               break;
     }

     return TABLE_STRING_FAIL;
}


/*******************************************************************
函数功能: 打印队列信息
输入参数: handle  队列头
输出参数: 无
*******************************************************************/
void table_string_show(void *handle)
{
     int i = 0;
#ifdef HAVE_SUPPORT_HEAP
     talbe_example *head_node = (talbe_example *)handle;
     table_item *me = 0;
     
     if(TABLE_STRING_NULL != head_node)
          me = head_node->table_data;
#else
     table_item *me = (table_item *)handle;
#endif

     for(i = 0; i < TALBE_LENGTH; i++)
     {
          if(me[i].status == TABLE_USING)
          {
               printf("%d: %s\n", i, me[i].c_data);
          }
          else
               break;
     }
     printf("\n");
}



#ifdef HAVE_FULL_MODULE
#undef malloc
#undef free
#endif

#ifdef HAVE_FULL_MODULE
void *table_string_malloc(int size)
{
     return malloc(size);
}


void table_string_free(void *handle)
{
     free(handle);
}

#endif




2012-05-23 18:45
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:0 
这个代码花了一个半小时,测试已经通过,能满足你的要求
拿去先玩玩吧
有什么不懂可以问我。
2012-05-23 18:45
快速回复:顺序表中查找问题
数据加载中...
 
   



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

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