符号表的索引实现-使用3个数组实现
2014-05-16 17:28
120 查看
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedefchar* itemType;
typedefchar* keyType;
/*先查找字符串是否存在树中,如果不存在,就插入,如果存在,就不插入*/
static int N;
static int inc;
static itemType a[100];//数据
staticint left[100];//左边数组
staticint right[100];//右边数组
//#define realKey(a[A]) (a[A])
#define key(A) (a[A])
#define realKey(A) A
#define KNull -3
#define KContain -2
#define KEqual -4
#define KLess -1
#define KMore -5
void STinit(int maxN)
{
N = maxN;
for (int i =
0;i < N;++i)
{
left[i] = 0;
right[i] = 0;
}
}
int STcount()
{
return inc;
}
int isContain(keyType aFirst,keyType aSecond)
{
if (strcmp(aFirst,aSecond) ==
0)
{
return KEqual;
}
if (strlen(aFirst) <=
strlen(aSecond))
{
for (int i =
0;i < strlen(aSecond); ++i)
{
int temp = i;
int j = 0;
for (;j < strlen(aFirst);++j)
{
if (aFirst[j] == aSecond[temp])
{
++temp;
continue;
}
else
{
break;
}
}
if (j == strlen(aFirst))
{
return KContain;
}
}
}
if (strcmp(aFirst,aSecond) <
0)
{
return KLess;
}
else if (strcmp(aFirst,aSecond) >
0)
{
return KMore;
}
return KNull;
}
int searchR(keyType aKey)
{
int i = 0;
int count = 0;
while (a[i])
{
int tmp = isContain(aKey,key(i));
if (tmp == KEqual||tmp ==
KContain)
{
return tmp;
}
else if (tmp ==
KLess)
{
i =
left[i];
if (i == 0)
{
break;
}
}
else if (tmp ==
KMore)
{
i =
right[i];
if (i == 0)
{
break;
}
}
count = i;
}
return count;
}
int STsearch(keyType aKey)
{
return searchR(aKey);
}
void STinsert(itemType aItem)
{
int i = STsearch(realKey(aItem));
if (i >= 0)
{
int j = 0;
while (a[j])
{
j +=1;
}
while (!a[j])
{
a[j] = malloc(1+sizeof(char)*strlen(realKey(aItem)));
}
strcpy(a[j],realKey(aItem));
if (inc)
{
int tmp = isContain(realKey(aItem),a[i]);
if(tmp == KLess)
{
right[i] = j;
}
else if(tmp ==
KMore)
{
left[i] = j;
}
}
inc++;
}
}
#include <string.h>
#include <stdlib.h>
typedefchar* itemType;
typedefchar* keyType;
/*先查找字符串是否存在树中,如果不存在,就插入,如果存在,就不插入*/
static int N;
static int inc;
static itemType a[100];//数据
staticint left[100];//左边数组
staticint right[100];//右边数组
//#define realKey(a[A]) (a[A])
#define key(A) (a[A])
#define realKey(A) A
#define KNull -3
#define KContain -2
#define KEqual -4
#define KLess -1
#define KMore -5
void STinit(int maxN)
{
N = maxN;
for (int i =
0;i < N;++i)
{
left[i] = 0;
right[i] = 0;
}
}
int STcount()
{
return inc;
}
int isContain(keyType aFirst,keyType aSecond)
{
if (strcmp(aFirst,aSecond) ==
0)
{
return KEqual;
}
if (strlen(aFirst) <=
strlen(aSecond))
{
for (int i =
0;i < strlen(aSecond); ++i)
{
int temp = i;
int j = 0;
for (;j < strlen(aFirst);++j)
{
if (aFirst[j] == aSecond[temp])
{
++temp;
continue;
}
else
{
break;
}
}
if (j == strlen(aFirst))
{
return KContain;
}
}
}
if (strcmp(aFirst,aSecond) <
0)
{
return KLess;
}
else if (strcmp(aFirst,aSecond) >
0)
{
return KMore;
}
return KNull;
}
int searchR(keyType aKey)
{
int i = 0;
int count = 0;
while (a[i])
{
int tmp = isContain(aKey,key(i));
if (tmp == KEqual||tmp ==
KContain)
{
return tmp;
}
else if (tmp ==
KLess)
{
i =
left[i];
if (i == 0)
{
break;
}
}
else if (tmp ==
KMore)
{
i =
right[i];
if (i == 0)
{
break;
}
}
count = i;
}
return count;
}
int STsearch(keyType aKey)
{
return searchR(aKey);
}
void STinsert(itemType aItem)
{
int i = STsearch(realKey(aItem));
if (i >= 0)
{
int j = 0;
while (a[j])
{
j +=1;
}
while (!a[j])
{
a[j] = malloc(1+sizeof(char)*strlen(realKey(aItem)));
}
strcpy(a[j],realKey(aItem));
if (inc)
{
int tmp = isContain(realKey(aItem),a[i]);
if(tmp == KLess)
{
right[i] = j;
}
else if(tmp ==
KMore)
{
left[i] = j;
}
}
inc++;
}
}
相关文章推荐
- cc150:使用一个数组实现3个栈
- 数据结构实现之有序符号表BinarySearchST(使用有序数组的二分查找)
- LINQ的经典例子-Where,Select、SelectMany、SkipWhile子句中使用数组索引
- LINQ的经典例子-Where,Select、SelectMany、SkipWhile子句中使用数组索引
- PHP Array -- 使用数组实现矩阵的数学运算 -- PHP Matrix Math Functions
- 用泛型实现在数组模拟入栈出栈时多类型的使用
- [转] LINQ的经典例子-Where,Select、SelectMany、SkipWhile子句中使用数组索引。
- SQL Server2000 索引结构及其使用 (实现小数据量和海量数据的通用分页显示存储过程)
- 汇编语言使用加减法实现有符号数除法
- LINQ的经典例子-Where,Select、SelectMany、SkipWhile子句中使用数组索引
- 使用存储过程实现循环更新3组数组中的数据
- SQL Server使用索引实现数据访问优化
- 使用数组实现快速数据查找
- Flex与.NET互操作(十五):使用FluorineFx中的字节数组(ByteArray)实现图片上传
- C_综合使用数组实现简单的学生成绩管理系统
- Flex与.NET互操作(十五):使用FluorineFx中的字节数组(ByteArray)实现图片上传
- LINQ的经典例子-Where,Select、SelectMany、SkipWhile子句中使用数组索引
- SQLServer-存储过程中使用字符串和分隔符实现传递数组参数
- 使用泛型在VB.NET中实现VB6.0中的控件数组
- LINQ的经典例子-Where,Select、SelectMany、SkipWhile子句中使用数组索引