符号表的索引实现-使用3个数组实现

本文介绍了一种基于字符串比较的搜索与插入算法实现,通过定义特定的数据结构来完成字符串的高效搜索与插入操作。该算法适用于需要频繁进行字符串匹配与添加的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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++;

    }


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值