1. 程式人生 > >C語言實現數據結構串(堆分配存儲表示法)

C語言實現數據結構串(堆分配存儲表示法)

+= 賦值 size ++ fine hello n) clu 刪除字符串

————————————————————————————————————————————

  • 堆分配存儲表示法

————————————————————————————————————————————

存儲結構:

構建堆來存儲字符串,本質上是順序表

技術分享

————————————————————————————————————————————

實現代碼:

  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 }

C語言實現數據結構串(堆分配存儲表示法)