————————————————————————————————————————————
- 堆分配存储表示法
————————————————————————————————————————————
存储结构:
构建堆来存储字符串,本质上是顺序表
————————————————————————————————————————————
实现代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 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 int Status; 12 typedef struct 13 { 14 char *ch; //空串时指向NULL,非空串时按串长分配存储区 15 int length; 16 } HString; 17 Status InitString(HString *T) //初始化字符串 18 { 19 //指针指向NULL,长度为0即可 20 //p.s.申请内存空间的过程在赋值中完成 21 T->ch = NULL; 22 T->length = 0; 23 return OK; 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 return ERROR; 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 return OK; 49 } 50 } 51 Status StrPrint(HString T) //打印字符串 52 { 53 //通过长度判断打印的字符数 54 int i; 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 return T.length; 62 } 63 Status StrEmpty(HString T) //字符串判空 64 { 65 if (T.length == 0) 66 return TRUE; 67 else 68 return FALSE; 69 } 70 Status Concat(HString *T, HString S1, HString S2) //字符串联接 71 { 72 //1.申请长度为S1和S2之和的字符串空间 73 //2.先将S1的元素逐个赋值到T中 74 //3.再将S2的元素逐个赋值到T中 75 int i; 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 return OK; 87 } 88 Status StrDelete(HString *T, int pos, int len) //删除字符串中某个位置固定长度的子串 89 { 90 //pos是字符串中的位置,删除包括pos的len长度 91 int i; 92 if (pos >= T->length) 93 return ERROR; 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 return OK; 103 } 104 Status StrInsert(HString *S, int pos, HString T) 105 { 106 //pos是字符串中的位置,插入时原来的元素(包括pos位)后移 107 int i, 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 return OK; 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 return ERROR; 129 int i = pos - 1, j = 0; 130 while(i < S.length && j < T.length) 131 { 132 if (S.ch[i] == T.ch[j]) 133 { 134 ++i; 135 ++j; 136 } 137 else 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 return ERROR; 156 //当pos存在时循环,当全部索引完毕后pos为0 157 //将索引到的该位置对应的子串删除后再插入新的子串 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 return OK; 169 } 170 Status SubString(HString *Sub, HString S, int pos, int len) 171 { 172 int i; 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 return OK; 191 } 192 int main() 193 { 194 int pos; 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 return OK; 257 }
时间: 2024-10-26 19:37:38