数据结构c语言版(串)
第四章 串 通常将子串在主串中首次出现时的该子串的首字符对应的主串中的序号,定义为子串在主串中的序号(或位置)。例如,设A和B分别为 A=“This is a string” B=“is” 则B是A的子串,A为主串。B在A中出现了两次,其中首次出现所对应的主串位置是3。因此称B在A中的序号(或位置)为3 求子串: Status SubString(SString &Sub, SString S, int pos, int len) { // 用Sub返回串S的第pos个字符起长度为len的子串。 //其中,1≤pos≤StrLength(S)且0≤len≤StrLength(S)-//pos+1 。 if ( pos<1 || pos>S[0] || len<0 || len>S[0]-pos+1 ) return ERROR; Sub[1..len] = S[pos..pos+len-1]; Sub[0] = len; return OK; }//SubString for ( i=S.length-1;i>=pos-1; --1) //为插入T而腾出位置 S.ch[i+T.length] = S.ch[i]; S.ch[pos-1..pos+T.length-2] = T.ch[0..T.length-1]; //插入T S.Length += T.length; } return OK; } // StrInsert //====ADT String的表示与实现==== //----串的堆分配存储表示---- Typedef struct{ char *ch ; //若是非空串,则按串长分配存储区,否则ch为NULL int length; //串长度 }HString; //----基本操作的函数原型说明---- Status StrAssign ( HString &T, char *chars ); //生成一个其值等于串常量chars的串T int StrLength ( HString S ); //返回S的元素个数,称为串的长度 int StrCompare (HString S, HString T ) //若S>T,则返回值>0;若S=T,则返回值=0;若S