串的存储结构

#include <bits/stdc++.h>
using namespace std;

#define MAXLEN 255 //预定最大串长为255
typedef struct{
    char ch[MAXLEN]; //每个分量存储一个字符
    int length;//串的实际长度
}SString;

typedef struct{
    char *ch; //按串长分配存储区,ch指向串的基地址
    int length;//串长
}HString;

HString S;
S.ch = (char *)malloc(MAXLEN * sizeof(char));
S.length = 0;

signed main()
{

}
#include <bits/stdc++.h>
using namespace std;

typedef struct StringNode //存储密度低
{
    char ch;
    struct StringNode *next;
}StringNode, *String;

typedef struct StringNode{ //存储密度大
    char ch[4];
    struct StringNode *next;
}StringNode, *String;

signed main()
{

}
#include <bits/stdc++.h>
using namespace std;

#define MAXLEN 255
typedef struct{
    char ch[MAXLEN];
    int length;
}SString;

//求子串
bool SubString (SString &Sub , SString S ,int pos ,int len)
{
    //子串范围越界
    if(pos+len-1 > S.length)
        return false;
    for(int i = pos ; i < pos +len ; i++)
    {
        Sub.ch[i-pos+1] = S.ch[i];
    }
    Sub.length = len;
    return true;
}

//比较操作
int StrCompare(SString S,SString T)
{
    for(int i = 1 ; i < S.length && i <= T.length ; i++)
    {
        if(S.ch[i] != T.ch[i])
            return S.ch[i]-T.ch[i];
    }
    //扫描过的所有字符都相同,则长度长的串更大
    return S.length-T.length;
}

//若主串S中存在与子串T相同的子串,返回第一次出现的位置
int Index(SString S,SString T)
{
    int i = 1 ,n = StrLength(S),m = StrLength(T);
    SString sub;
    while(i<=n-m+1)
    {
        SubString(sub,S,i,m);
        if(StrCompare(sub,T) != 0) ++i;
        else return i;
    }
    return 0;
}

signed main()
{

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值