2.堆分配存信表示 以一组地址连续的存储单元存放串值字 符序列; 存储空间动态分配,用maoc(和free0 来管理
2.堆分配存储表示 以一组地址连续的存储单元存放串值字 符序列; 存储空间动态分配,用malloc()和free() 来管理
3.牛的块链存储表示 串的链式存储方式 结点大小:一个或多个字符 nP78图42(a)(b) 存储密度=串值所占的存储位(实际分配的存储位
3.串的块链存储表示 串的链式存储方式 结点大小:一个或多个字符 ▪ P78图4.2 (a) (b) ▪ 存储密度=串值所占的存储位/实际分配的存储位
4.的基本操作 串插入 Status strlnsert( HString&s, int pos, HString T 串赋值 Status StrAssign( HString&s, char*chars) 求串长 int StrEngth( HString S 串比较 int StrCompare( HString S, HString T 串联接 Status Concat(( HString&s, HString S1, HString S2) 求子串 Status SubString( HString&sub, HString S, int pos, int len) 串清空 Status Clear String( HString&S) 串定位 删除 置换
4.串的基本操作 串插入 Status StrInsert(HString &S,int pos,HString T) 串赋值 Status StrAssign(HString &S,char *chars) 求串长 int StrLength(HString S) 串比较 int StrCompare(HString S,HString T) 串联接 Status Concat(HString &S,HString S1,HString S2) 求子串 Status SubString(HString &Sub,HString S,int pos,int len) 串清空 Status ClearString(HString &S) 串定位 删除 置换
Status StrInsert(HString &s, int pos, HString T) 在串5的第pos个位置前插入5 if (pos <1 pos>s length+1)return ERROR; if (T length)t if ((sch=(char ealloc(sch, (S length+T length)*sizeof(char))) exit(OVERFLOW) for(i=s length-1; i>=pos-1; -i) sch[i+T length]=S ch[] for (i=0; K<=Tlength-1; i ++ sch[pos-1+iT.ch[] Slength+=Tlength; s return OK
Status StrInsert(HString &S,int pos,HString T) //在串S的第pos个位置前插入串S { int i; 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=S.length-1;i>=pos-1;--i){ S.ch[i+T.length]=S.ch[i]; } for (i=0; i<=T.length-1;i++) S.ch[pos-1+i]=T.ch[i]; S.length+=T.length; } return OK; }
Status StrAssign(HString &S, char chars) 生成一个值等 chars的生5 int i,j; char *C, for(i=0, c=chars; C: ++i, ++c) if(iIs. ch=NULL,S length=0; 1 else i if((sch=(char *)malloc(i *sizeof(char))) exit(OVERFLOW for(j=0j<=i-1j++){ s.chjcharsl Slength=i eturn OK
Status StrAssign(HString &S,char *chars) 生成一个值等于chars的串S { int i,j; char *c; for (i=0,c=chars;*c;++i,++c); if (!i) {S.ch=NULL; S.length=0;} else { if (!(S.ch=(char *)malloc(i * sizeof(char)))) exit(OVERFLOW); for (j=0;j<=i-1;j++){ S.ch[j]=chars[j];} S.length=i; } return OK; }