数据结构串之堆串

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

#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]!='')
	{
		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;
} 

  

亲爱的听众朋友我是你的代班DJ
原文地址:https://www.cnblogs.com/YTYMblog/p/5409885.html