9、串的堆分配方式
1、代码实现
#include<stdio.h> #include<malloc.h> #include<assert.h> #include<string.h> typedef struct HeapString{ char* ch; int length; }HString; //初始化 void initHString(HString* S){ S->ch = NULL; S->length = 0; } //赋值 void strAssign(HString* S,char *str){ int len = strlen(str); int i = 0; //如果之前已经分配过空间,要先释放掉 if(S->ch != NULL){ free(S->ch); } S->ch = (char*)malloc(sizeof(char) * len); assert(S->ch != NULL); for(i;i < len;i++){ S->ch[i] = str[i]; } S->length = len; } //打印 void printString(HString *S){ int i = 0; for(i = 0;i < S->length;i++){ printf("%c",S->ch[i]); } printf("\n"); } //拷贝 void strCopy(HString *S,HString *T){ if(S->ch != NULL){ free(S->ch); } int len = T->length; S->ch = (char*)malloc(sizeof(char) * len); assert(S->ch != NULL); int i = 0; for(i;i < len;i++){ S->ch[i] = T->ch[i]; } S->length = len; } //判空 int strEmpty(HString *S) { return S->length == 0; } //比较 int strCompare(HString *S,HString *T){ if(S->length == 0 && T->length == 0) return; int result = 0; int i = 0; int j = 0; while(i < S->length && j < T->length){ if(S->ch[i] > T->ch[j]){ return 1; } else if(S->ch[i] < T->ch[j]){ return -1; } else { i++; j++; } } if(i < S->length) result = 1; if(j < T->length) result = -1; return result; } int StrLength(HString* S){ return S->length; } void strConcat(HString *T,HString *s1,HString *s2){ if(T->ch != NULL) free(T->ch); int len1 = s1->length; int len2 = s2->length; T->ch = (char*)malloc(sizeof(char) * (len1 + len2)); assert(T->ch != NULL); int i = 0; for(i = 0;i < len1;i++){ T->ch[i] = s1->ch[i]; } int j = 0; for(j;j < len2;j++){ T->ch[i + j] = s2->ch[j]; } T->length = len1 + len2; } void subString(HString *S,HString *sub,int pos,int len){ if(pos < 0 || pos > S->length || len <= 0 || len > S->length - pos) return; if(sub->ch != NULL) free(sub->ch); sub->ch = (char*)malloc(sizeof(char) * len); assert(sub->ch != NULL); int i = 0; for(i;i < len;i++){ sub->ch[i] = S->ch[pos + i]; } sub->length = len; } void strInsert(HString *S,int pos,HString *T){ if(T->length == 0) return; if(pos < 0 || pos > S->length) return; char* ch = (char*)realloc(S->ch,sizeof(char) * (S->length + T->length)); assert(ch != NULL); S->ch = ch; //先挪数据,从S的最后一个字符开始挪动,挪动距离为T的长度 int i = S->length - 1; for(i;i >= pos;i--){ S->ch[i + T->length] = S->ch[i]; } for(i = 0;i < T->length;i++){ S->ch[pos + i] = T->ch[i]; } S->length += T->length; } void strDelete(HString *S,int pos,int len){ if(pos < 0 || pos > S->length) return; if(len <= 0 || len > S->length - pos) return; int i = 0; for(i;i < len;i++){ S->ch[pos + i] = S->ch[pos + i + len]; } } void strClear(HString *S){ S->length = 0; if(S->ch != NULL) free(S->ch); S->ch = NULL; } int main(){ HString S; initHString(&S); strAssign(&S,"abcdefg"); HString T; initHString(&T); strAssign(&T,"123"); int res = strCompare(&S,&T); HString Y; initHString(&Y); // strConcat(&Y,&S,&T); // subString(&S,&Y,2,3); strInsert(&S,2,&T); // printString(&Y); printString(&S); return 0; }#include<stdio.h> #include<malloc.h> #include<assert.h> #include<string.h> typedef struct HeapString{ char* ch; int length; }HString; //初始化 void initHString(HString* S){ S->ch = NULL; S->length = 0; } //赋值 void strAssign(HString* S,char *str){ int len = strlen(str); int i = 0; //如果之前已经分配过空间,要先释放掉 if(S->ch != NULL){ free(S->ch); } S->ch = (char*)malloc(sizeof(char) * len); assert(S->ch != NULL); for(i;i < len;i++){ S->ch[i] = str[i]; } S->length = len; } //打印 void printString(HString *S){ int i = 0; for(i = 0;i < S->length;i++){ printf("%c",S->ch[i]); } printf("\n"); } //拷贝 void strCopy(HString *S,HString *T){ if(S->ch != NULL){ free(S->ch); } int len = T->length; S->ch = (char*)malloc(sizeof(char) * len); assert(S->ch != NULL); int i = 0; for(i;i < len;i++){ S->ch[i] = T->ch[i]; } S->length = len; } //判空 int strEmpty(HString *S) { return S->length == 0; } //比较 int strCompare(HString *S,HString *T){ if(S->length == 0 && T->length == 0) return; int result = 0; int i = 0; int j = 0; while(i < S->length && j < T->length){ if(S->ch[i] > T->ch[j]){ return 1; } else if(S->ch[i] < T->ch[j]){ return -1; } else { i++; j++; } } if(i < S->length) result = 1; if(j < T->length) result = -1; return result; } int StrLength(HString* S){ return S->length; } void strConcat(HString *T,HString *s1,HString *s2){ if(T->ch != NULL) free(T->ch); int len1 = s1->length; int len2 = s2->length; T->ch = (char*)malloc(sizeof(char) * (len1 + len2)); assert(T->ch != NULL); int i = 0; for(i = 0;i < len1;i++){ T->ch[i] = s1->ch[i]; } int j = 0; for(j;j < len2;j++){ T->ch[i + j] = s2->ch[j]; } T->length = len1 + len2; } void subString(HString *S,HString *sub,int pos,int len){ if(pos < 0 || pos > S->length || len <= 0 || len > S->length - pos) return; if(sub->ch != NULL) free(sub->ch); sub->ch = (char*)malloc(sizeof(char) * len); assert(sub->ch != NULL); int i = 0; for(i;i < len;i++){ sub->ch[i] = S->ch[pos + i]; } sub->length = len; } void strInsert(HString *S,int pos,HString *T){ if(T->length == 0) return; if(pos < 0 || pos > S->length) return; char* ch = (char*)realloc(S->ch,sizeof(char) * (S->length + T->length)); assert(ch != NULL); S->ch = ch; //先挪数据,从S的最后一个字符开始挪动,挪动距离为T的长度 int i = S->length - 1; for(i;i >= pos;i--){ S->ch[i + T->length] = S->ch[i]; } for(i = 0;i < T->length;i++){ S->ch[pos + i] = T->ch[i]; } S->length += T->length; } void strDelete(HString *S,int pos,int len){ if(pos < 0 || pos > S->length) return; if(len <= 0 || len > S->length - pos) return; int i = 0; for(i;i < len;i++){ S->ch[pos + i] = S->ch[pos + i + len]; } } void strClear(HString *S){ S->length = 0; if(S->ch != NULL) free(S->ch); S->ch = NULL; } int main(){ HString S; initHString(&S); strAssign(&S,"abcdefg"); HString T; initHString(&T); strAssign(&T,"123"); int res = strCompare(&S,&T); HString Y; initHString(&Y); // strConcat(&Y,&S,&T); // subString(&S,&Y,2,3); strInsert(&S,2,&T); // printString(&Y); printString(&S); return 0; }