第四章 串.PPT
文本预览下载声明
* (10)串插入 Status StrInsert(SString S,int pos,SString T) S T pos T[0] MAXSTRLEN (b) S [0]MAXSTRLEN而 S [0]+T[0]MAXSTRLEN for(i=pos;ipos+T[0];i++) S[i]=T[i-pos+1]; for(i=MAXSTRLEN;i=pos+T[0];i--) S[i]=S[i-T[0]] * Status StrInsert(SString S,int pos,SString T) { if(pos1||posS[0]+1) return ERROR; if(S[0]+T[0]=MAXSTRLEN) { // 完全插入 for(i=S[0];i=pos;i--) S[i+T[0]]=S[i]; for(i=pos;ipos+T[0];i++) S[i]=T[i-pos+1]; S[0]=S[0]+T[0]; return TRUE; } else { // 部分插入 for(i=MAXSTRLEN;i=pos+T[0];i--) S[i]=S[i-T[0]]; for(i=pos;ipos+T[0];i++) S[i]=T[i-pos+1]; S[0]=MAXSTRLEN; return FALSE; } } * Status StrDelete(HString S,int pos,int len){ (11)串删除 S pos S[0] for(i=pos+len;i=S[0];i++) S[i-len]=S[i]; pos+len-1 * Status StrDelete(SString S,int pos,int len) { //从串S中删除第pos个字符起长度为len的子串 if(pos1||pos+len-1S[0] || len0) return ERROR; for(i=pos+len;i=S[0];i++) S[i-len]=S[i]; S[0]-=len; return OK; } (11)串删除 * Status Replace(SString S, SString T, SString V) S S[0] (12)串替换 T V V[0] (1) i=Index(S, T, i); (2) StrDelete(S, i, StrLength(T)); (3) StrInsert(S, i, V); * Status Replace(SString S, SString T, SString V) S S[0] (12)串替换 T V V[0] (1) i=Index(S, T, i); (2) StrDelete(S, i, StrLength(T)); (3) StrInsert(S, i, V); * Status Replace(SString S,SString T,SString V) { // 用V替换主串S中出现的所有与T相等的不重叠的子串 int i=1; // 从串S的第一个字符起查找串T if(StrEmpty(T)) return ERROR; // T是空串 do{ i=Index(S,T,i); // 结果i为从上一个i之后找到的子串T的位置 if(i){// 串S中存在串T StrDelete(S,i,StrLength(T)); // 删除该串T StrInsert(S,i,V); // 在原串T的位置插入串V i+=StrLength(V); // 在插入的串V后面继续查找串T } }while(i); return OK; } (12)串替换 * 4.2.2 堆分配存储表示 typedef struct{ char *ch; //若串非空,则按串长分配存储区, //否则ch为NULL int length; //串长度 }HString; * (1) 串初始化 St
显示全部