数据结构串之堆串

本文介绍了堆串的基本概念及其常用操作,包括插入、删除和赋值等,并提供了详细的C语言实现代码。

这是堆串,以后有时间写总结,都是自己已经敲出来的

#include<stdio.h>
//char* ch表示串的起始地址,len表示串的长度 
typedef struct DUI{
	char* ch;
	int len;
}HString;
//堆串插入函数
//将t串插入s串中pos位置前 
bool StrInsert(HString* s,int pos,HString* t)
{
	int i;
	char* temp;
	if(pos<0||s->len==0||pos>s->len)
		return false;
	temp=(char*)malloc(s->len+t->len);
	if(temp==NULL)
		return false;
	for(i=0;i<pos;i++)
		temp[i]=s->ch[i];
	for(i=0;i<t->len;i++)
		temp[i+pos]=t->ch[i];
	for(i=pos;i<s->len;i++)
		temp[i+t->len]=s->ch[i];\
	s->len+=t->len;
	free(s->ch);
	s->ch=temp;
	return true;
}

//在串s中删除从下标pos起len个字符
bool StrDelete(HString* s,int len,int pos)
{
	int i;
	char* temp;
	if(pos<0||pos+len>s->len)
	return false;
	temp=(char*)malloc(s->len-len));
	if(temp==NULL)
	return false;
	for(i=0;i<pos;i++)
	{
		temp[i]=s->ch[i];
	}
	for(i=pos;i<s->len-len;i++)
		temp[i]=s->ch[i+len];
	s->len=s->len-len;
	free(s->ch);
	s->ch=temp;
	return true;
}
 
//堆串赋值函数
//将字符串tval的值赋给堆串s 
bool StrAssign(HString* s,char* tval)
{
	int i=0,len;
	if(s->ch!=NULL)
		free(ch);
	while(tval[i]!='\0')
	{
		i++;
	}
	len=i;
	if(len)
	{
		s->ch=(chat*)malloc(len);
		if(s->ch=NULL)
			return false;
		for(i=0;i<len;i++)
			s->ch[i]=tval[i];
	}
	else
		s->ch=NULL;
	s->len=len;
	return true;
} 

  

转载于:https://www.cnblogs.com/YTYMblog/p/5409885.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值