第四章串——4.2:串的表现和实现

第四章串——4.2:串的表现和实现

我们对于串的定长顺序表示如下

#define chushi 255
typedef unsigned char sstring[chushi + 1];

例4.2我们首先进行串的连接

Status concat(sstring& t, sstring s1, sstring s2)
{
	if (s1[0] + s2[0] <= chushi)
	{
		t[1..s1[0]] = s1[1..s1[0]];
		t[s1[0] + 1..s2[0]] = s2[1..s2[0]];
		t[0] = s1[0] + s2[0];
		uncut = true;
	}
	else if (s1[0] < chushi)
	{
		t[1..s1[0]] = s1[1..s1[0]];
		t[s1[0] + 1..chushi] = s2[1..shushi - s1[0]];
		t[0] = chushi;
		uncut = fault;
	}
	else
	{
		t[0..chushi] = s1[0..chushi];
		uncut = fault;
	}
	return fault;
}

求子串

Status substring(sstring& sub, sstring s, int pos, int len)
{
	if (pos<1 || pos>s[0] || len<0 || len>s[0] - pos + 1)
		return error;
	sub[1..len] = s[pos..pos + len - 1];
	sub[0] = len;
	return ok;
}

为避免串的长度有上限的问题,我们使用段的堆分配存储

typedef struct
{
	char* ch;
	int length;
}hstring;

例4.4在串s的第pos个字符前插入串t

Status strinsert(hstring& s, int pos, hstring t)
{
	if (pos<1 || pos>s.length + 1)return error;
	if (t.length)
	{
		if (!(s.ch = (char*)realloc(s.ch, (s.length + t.length) * sizeof(char))))
			exit(overflow);
		for (i = length - 1; i >= pos - 1; --i)
			s.ch[i.t.length] = s.ch[i];
		s.ch[pos - 1..pos_t.length - 2] = t.ch[0..t, length - 1];
		s.length += t.length;
	}
	return ok;
}

下面是一些串的堆分配的基本函数

生成一个值为chars的串t

Status strassign(hstring& t, char* chars)
{
	if (t.ch)free(t.ch);
	for (i = 0, c = chars; *c; ++i, ++c);
	if(!i)
		{
			t.ch=NULL;
			t.length=0;
		}
	else
	{
		if (!(t.ch = (char*)malloc(i * sizeof(char))))
			exit(overflow);
		t.ch[0..i - 1] = chars[0..i - 1];
		t.length = i;
	}
	return ok;
}

返回s的长度

int strlength(hstring& s)
{
	return t.length;
}

若s>t返回值>0,

int strcompare(hstring s, hstring t)
{
	for (i = 0; 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清空

Status clearstring(hstring& s)
{
	if (s.ch)
	{
		free(s.ch);
		s.ch = NULL;
	}
	s.length = 0;
	return ok;
}

返回s1和s1组成的新串

Status concat(hstring& t, hstring s1, hstring s2)
{
	if (t.ch)free(t.ch);
	if (!(t.ch = (char*)malloc(s1.length + s2.length) * sizeof(char)))
		exir(overflow);
	t.ch[0..s1.length - 1] = s1.ch[0..s1.length - 1];
	t.length = s1.length + s2.length;
	t.ch[s1.length - 1..t.length - 1] = s2.ch[0..s2.length - 1];
	return ok;
}

用sub返回s的dipos字符起长度为len的串

Status substring(Hstring& sub, hstring s, int pos, int len)
{
	if (pos<1 || pos>s.length || len<0 || len>s.length - pos + 1)
	{
		return error;
	}
	if (sub.ch) free(sub.ch);
	if (!len)
	{
		sub.ch = NULL;
		sub.length = 0;
	}
	else
	{
		sub.ch = (char*)malloc(len * sizeof(char));
		sub.ch[0..len - 1] = s.ch[pos - 1..pos + len - 2];
		sub, length = len;
	}
	return ok;
}

串还有块链存储

#define chushi 80
typedef struct chunk
{
	char ch[chushi];
	struct chunk* next;
}chunk;
typedef struct
{
	chunk* head, * tail;
	int curlen;
}lstring;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Harden先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值