数据结构——串的堆分配存储

#include <iostream>
using namespace std;

typedef
struct
{
char *ch;
int len;
}HString;

//建立串
void BuildStr(HString &s, char *str)
{
int length=strlen(str);
s.ch
=(char *)malloc(length*sizeof(char));
int i=0;
while(str[i]!='\0')
{
s.ch[i]
=str[i];
i
++;
}
s.ch[i]
='\0';
}
//求串的长度
int StrLength(HString &hs)
{
int i=0;
while(hs.ch[i]!='\0')
i
++;
hs.len
=i;
return hs.len;
}
//输出串
void Print_HString(HString &hs)
{
int i=0;
while(hs.ch[i]!='\0')
{
cout
<<hs.ch[i];
i
++;
}
}
//串复制
/*

void StrCopy(HString &to,HString &from)
{
if(to.ch)
{
to.ch=NULL;
to.len=0;
}
int i;
char *temp=(char *)malloc(from.len * sizeof(char));
for(i=0; i<from.len; i++)
{
//temp[i] = from.ch[i];
*(temp+i) = from.ch[i];
}
to.ch=temp;
to.len = from.len;
//to.ch[to.len]='\0';
*(to.ch+to.len)='\0';
}
*/

//串连接
/*

void Strcat(HString &to,HString &from)
{

char *temp=(char *)malloc((to.len+from.len)*sizeof(char) );
if(!temp)
cout<<"内存分配失败!"<<endl;
int i;
for(i=0; i<to.len; i++)
{
temp[i] = to.ch[i];
}
for(i=0; i<from.len; i++)
{
temp[to.len+i] = from.ch[i];
}
to.len=to.len+from.len;
to.ch=temp;
*(to.ch+to.len)='\0';
}
*/
//串比较
/*

int strcmp(HString &s1,HString &s2)
{
int i;
for(i=0; i<=s1.len; i++)
{
if(s1.ch[i]!=s2.ch[i])
return s1.ch[i]-s2.ch[i];
}
return 0;
}
*/
//字符定位
/*

void StrLocate(HString &s,char c)
{
int i;
for(i=0; i<s.len; i++)
{
if(s.ch[i]==c)
{
cout<<"位置:"<<i<<endl;
break;
}
}
if(i==s.len)
cout<<"查无此人!"<<endl;
}
*/

//求s中从第index个字符开始长度为len的子串
/*

HString StrSub(HString &s, int index, int len)
{
HString temp;
temp.len=0;
temp.ch =(char *)malloc( len*sizeof(char) );
if(index+len>s.len)
{
cout<<"提取的子串过长!"<<endl;
}
else
{
int i;
for(i=0;i<len;i++)
temp.ch[i]=s.ch[index+i];
temp.len=len;
temp.ch[temp.len] = '\0';
}
return temp;
}
*/
//删除s中从第index个字符开始长度为len的子串
/*

void StrDel(HString &s, int index, int len)
{
int i;
if(index+len>=s.len)
s.len=index;
else
{
for(i=index+len;i<s.len;i++)
s.ch[i-len]=s.ch[i];
s.len=s.len-len;
}
s.ch[s.len]='\0';
}
*/
//向串s中第index个位置插入串t

void StrInsert(HString &s, int index, HString &t)
{
if(index>s.len)
cout
<<"插入位置不对!"<<endl;
else
{
s.ch
=(char *)realloc(s.ch , (s.len+t.len)*sizeof(char)); //新增加内存
int i,j=1;
for(i=s.len+t.len-1;i>=index+t.len;i--)
{
s.ch[i]
=s.ch[s.len-j];
j
++;
}
for(i=0;i<t.len;i++)
{
s.ch[index
+i]=t.ch[i];
}
s.len
=s.len+t.len;
s.ch[s.len]
='\0';
}
}
//s串中从第 index 个字符开始的 len 个连续字符将被 t 替换
void StrRep(HString &s, int index, int len, HString &t)
{
int i;
for(i=0;i<len;i++)
s.ch[index
+i]=t.ch[i];
s.len
=(index+i)>s.len ? (index+i) : s.len;
s.ch[s.len]
='\0';
}

int main()
{
HString hs1;
char *str="Hello World!";
BuildStr(hs1,str);

//输出串
StrLength(hs1);//求串的长度
cout<<hs1.len<<endl;
Print_HString(hs1);
cout
<<endl;

HString hs2;
char *str2="世界你好";
BuildStr(hs2,str2);
//输出串
StrLength(hs2);
cout
<<hs2.len<<endl;
Print_HString(hs2);
cout
<<endl;

//串复制
/*
HString hs3;
hs3.ch="世界你好"; //如果目标串已有字符,则删除
StrCopy(hs3,hs1);
cout<<hs3.len<<endl;
Print_HString(hs3);
cout<<endl;
*/

//串连接
//Strcat(hs1,hs2);
//cout<<hs1.len<<endl;
//Print_HString(hs1);
//cout<<endl;

//串1和串2比较
/*
int result=strcmp(hs1,hs2);
if(result>0)
cout<<"串1大于串2"<<endl;
else if(result==0)
cout<<"串1等于串2"<<endl;
else
cout<<"串1小于串2"<<endl;
*/

//字符定位
//char ch;
//cin>>ch;
//StrLocate(hs1,ch);

//提取hs1中的子串
//cout<<"提取的子串是:"<<endl;
//Print_HString(StrSub(hs2, 4, 4));
//cout<<endl;

//删除S1中的子串
//StrDel(hs1, 5, 8);
//cout<<"删除后的串是:"<<endl;
//Print_HString(hs1);
//cout<<endl;

//向串S1中第index个位置插入串t
StrInsert(hs1, 5, hs2);
cout
<<hs1.len<<endl;
Print_HString(hs1);
cout
<<endl;

//替换S1串中从第 index 个字符开始的 len 个连续字符
HString T;
char *str3="********";
BuildStr(T,str3);
StrRep(hs1,
13, 8, T);
cout
<<hs1.len<<endl;
Print_HString(hs1);
cout
<<endl;
return 0;
}

 

posted @ 2010-07-28 10:54  忧国忧铭  Views(970)  Comments(0)    收藏  举报