C语言串的堆分配存储结构,C语言实现数据结构串(堆分配存储表示法)

1 #include

2 #include

3 #include

4 #define OK 1

5 #define ERROR 0

6 #define TRUE 1

7 #define FALSE 0

8 #define OVERFLOW -2

9 #define STR_INIT_SIZE 100

10 #define STRINCREMENT 10

11 typedef intStatus;12 typedef struct

13 {14 char *ch; //空串时指向NULL,非空串时按串长分配存储区

15 intlength;16 } HString;17 Status InitString(HString *T) //初始化字符串

18 {19 //指针指向NULL,长度为0即可20 //p.s.申请内存空间的过程在赋值中完成

21 T->ch =NULL;22 T->length = 0;23 returnOK;24 }25 Status StrAssign(HString *T, char *p) //字符串赋值

26 {27 //1.判断T是否已有内容,有则释放28 //2.判断赋值的内容是否为空,为空则不赋值29 //3.根据长度向内存申请空间,遍历赋值给T,长度等于字符串长度30 //p.s.在这里赋值不赋\0,在打印时通过长度来判断字符串结尾

31 int i, len =strlen(p);32 if (T->ch)33 free(T->ch);34 if (!len)35 {36 T->ch =NULL;37 T->length = 0;38 returnERROR;39 }40 else

41 {42 T->ch = (char *)malloc(len * sizeof(char));43 if(!T->ch)44 exit(OVERFLOW);45 for (i = 0; i < len; ++i)46 T->ch[i] =p[i];47 T->length =len;48 returnOK;49 }50 }51 Status StrPrint(HString T) //打印字符串

52 {53 //通过长度判断打印的字符数

54 inti;55 for (i = 0; i < T.length; ++i)56 printf("%c", T.ch[i]);57 printf("\n");58 }59 Status StrLength(HString T) //字符串长度

60 {61 returnT.length;62 }63 Status StrEmpty(HString T) //字符串判空

64 {65 if (T.length == 0)66 returnTRUE;67 else

68 returnFALSE;69 }70 Status Concat(HString *T, HString S1, HString S2) //字符串联接

71 {72 //1.申请长度为S1和S2之和的字符串空间73 //2.先将S1的元素逐个赋值到T中74 //3.再将S2的元素逐个赋值到T中

75 inti;76 if (T->ch)77 free(T->ch);78 T->ch = (char *)malloc((S1.length + S2.length) * sizeof(char));79 if (!T->ch)80 exit(OVERFLOW);81 for (i = 0; i < S1.length; ++i)82 T->ch[i] =S1.ch[i];83 for (i = 0; i < S2.length; ++i)84 T->ch[i + S1.length] =S2.ch[i];85 T->length = S1.length +S2.length;86 returnOK;87 }88 Status StrDelete(HString *T, int pos, int len) //删除字符串中某个位置固定长度的子串

89 {90 //pos是字符串中的位置,删除包括pos的len长度

91 inti;92 if (pos >= T->length)93 returnERROR;94 else if(pos + len > T->length)95 len = T->length - pos + 1;96 for (i = pos - 1; i < T->length - len; ++i)97 T->ch[i] = T->ch[i +len];98 T->length -=len;99 T->ch = (char *)realloc(T->ch, T->length * sizeof(char));100 if (!T->ch)101 exit(OVERFLOW);102 returnOK;103 }104 Status StrInsert(HString *S, intpos, HString T)105 {106 //pos是字符串中的位置,插入时原来的元素(包括pos位)后移

107 inti, len;108 --pos;109 len =StrLength(T);110 S->ch = (char *)realloc(S->ch, (S->length + len) * sizeof(char));111 if (pos > S->length)112 pos = S->length;113 for (i = S->length - 1; i > pos - 1; --i)114 S->ch[i + len] = S->ch[i];115 for (i = 0; i < len; ++i)116 S->ch[i + pos] =T.ch[i];117 S->length +=len;118 if (!S->ch)119 exit(OVERFLOW);120 returnOK;121 }122 Status Index(HString S, HString T, int pos) //在字符串S中索引位置pos之后的子串t

123 {124 //同定长顺序存储表示法125 //p.s.传入的pos是字符串的位置,从1开始126 //p.s.初始状态下T为非空串

127 if(StrEmpty(T))128 returnERROR;129 int i = pos - 1, j = 0;130 while(i < S.length && j

138 {139 i = i - j + 1;140 j = 0;141 }142 }143 if (j >=T.length)144 return i - j + 1;145 else

146 return 0;147 }148 Status Replace(HString *T, HString S1, HString S2) //将字符串T中等于S1的子串替换成为S2

149 {150 //循环索引子串S1在字符串T中的位置(每次的位置从上一次位置后开始查找)151 //从查找到的位置-1开始替换152 //p.s.初始状态下S1为非空串

153 int pos = 0;154 if(StrEmpty(S1))155 returnERROR;156 //当pos存在时循环,当全部索引完毕后pos为0157 //将索引到的该位置对应的子串删除后再插入新的子串

158 do

159 {160 pos = Index(*T, S1, pos);161 if(pos)162 {163 StrDelete(T, pos, StrLength(S1));164 StrInsert(T, pos, S2);165 }166 }167 while(pos);168 returnOK;169 }170 Status SubString(HString *Sub, HString S, int pos, intlen)171 {172 inti;173 if (pos < 1 || len > S.length || len < 0 || len > S.length - pos + 1)174 exit(OVERFLOW);175 if (Sub->ch)176 free(Sub->ch);177 //如果查询的长度为0,则子串置空

178 if (len == 0)179 {180 Sub->ch =NULL;181 Sub->length = 0;182 }183 else

184 {185 Sub->ch = (char *)malloc(len * sizeof(char));186 for (i = 0; i < len; ++i)187 Sub->ch[i] = S.ch[pos + i - 1];188 Sub->length =len;189 }190 returnOK;191 }192 intmain()193 {194 intpos;195 HString t, s, r;196 char *p = "Hello,String!", *q = "Bye,Bye!";197 printf("String *p: %s\n", p);198 InitString(&t);199 StrAssign(&t, p);200 printf("StrAssign... OK.\nString t :");201 StrPrint(t);202 printf("------------------------------\n");203 printf("StrLength... OK.\nString Length : %d\n", StrLength(t));204 printf("StrEmpty... OK.\n");205 if(StrEmpty(t))206 printf("String is Empty.\n");207 else

208 printf("String is not Empty.\n");209 printf("------------------------------\n");210 InitString(&s);211 StrAssign(&s, q);212 printf("String s :");213 StrPrint(s);214 printf("------------------------------\n");215 InitString(&r);216 Concat(&r, t, s);217 printf("Concat... OK.\n");218 printf("String r :");219 StrPrint(r);220 printf("------------------------------\n");221 printf("StrDelete... OK.\n");222 StrDelete(&r, 14, 4);223 printf("String r :");224 StrPrint(r);225 printf("------------------------------\n");226 printf("StrInsert... OK.\n");227 StrAssign(&t, "Bye,Bye,Bye!");228 StrInsert(&r, 14, t);229 printf("String r :");230 StrPrint(r);231 printf("------------------------------\n");232 StrAssign(&t, "ye");233 printf("Index...");234 StrPrint(t);235 pos = 1;236 while(pos)237 {238 pos = Index(r, t, pos + 1);239 if (!pos)240 break;241 printf("Position : %d\n", pos);242 }243 printf("------------------------------\n");244 StrAssign(&t, "ye");245 StrAssign(&s, "oo");246 Replace(&r, t, s);247 printf("Replace ye -> ooo ... OK.\n");248 printf("String r :");249 StrPrint(r);250 printf("------------------------------\n");251 SubString(&t, r, 7, 4);252 printf("SubString... OK.\n");253 printf("String SubString :");254 StrPrint(t);255 printf("------------------------------\n");256 returnOK;257 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值