串的堆分配c语言代码
时间: 2023-11-30 08:41:07 浏览: 38
下面是一个串的堆分配C语言代码的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
typedef struct HString {
char *ch; //指向字符串所在空间首地址
int length; //记录字符串长度
} HString;
Status StrAssign(HString *S, char *chars) {
int i, len;
char *c;
for (len = 0, c = chars; *c; ++len, ++c); //计算chars长度
if (!len) { //chars为空串
S->ch = NULL;
S->length = 0;
} else {
if (!(S->ch = (char *) malloc(len * sizeof(char)))) { //分配空间失败
return ERROR;
} else {
for (i = 0; i < len; ++i) { //复制chars到S->ch
S->ch[i] = chars[i];
}
S->length = len;
}
}
return OK;
}
Status StrCopy(HString *T, HString S) {
int i;
if (T->ch) { //T不为空串,释放T原有空间
free(T->ch);
}
if (!S.length) { //S为空串
T->ch = NULL;
T->length = 0;
} else {
if (!(T->ch = (char *) malloc(S.length * sizeof(char)))) { //分配空间失败
return ERROR;
} else {
for (i = 0; i < S.length; ++i) { //复制S到T
T->ch[i] = S.ch[i];
}
T->length = S.length;
}
}
return OK;
}
int StrCompare(HString S, HString T) {
int i;
for (i = 0; i < S.length && i < T.length; ++i) {
if (S.ch[i] != T.ch[i]) {
return S.ch[i] - T.ch[i];
}
}
return S.length - T.length;
}
int StrLength(HString S) {
return S.length;
}
Status ClearString(HString *S) {
if (S->ch) { //S不为空串,释放S原有空间
free(S->ch);
S->ch = NULL;
}
S->length = 0;
return OK;
}
Status Concat(HString *T, HString S1, HString S2) {
int i;
char *c;
if (T->ch) { //T不为空串,释放T原有空间
free(T->ch);
}
if (!(T->ch = (char *) malloc((S1.length + S2.length) * sizeof(char)))) { //分配空间失败
return ERROR;
} else {
for (i = 0, c = S1.ch; i < S1.length; ++i, ++c) { //复制S1到T
T->ch[i] = *c;
}
for (i = 0, c = S2.ch; i < S2.length; ++i, ++c) { //复制S2到T
T->ch[S1.length + i] = *c;
}
T->length = S1.length + S2.length;
}
return OK;
}
Status SubString(HString *Sub, HString S, int pos, int len) {
int i;
char *c;
if (pos < 1 || pos > S.length || len < 0 || pos + len - 1 > S.length) { //参数不合法
return ERROR;
}
if (Sub->ch) { //Sub不为空串,释放Sub原有空间
free(Sub->ch);
}
if (!len) { //Sub为空串
Sub->ch = NULL;
Sub->length = 0;
} else {
if (!(Sub->ch = (char *) malloc(len * sizeof(char)))) { //分配空间失败
return ERROR;
} else {
for (i = 0, c = S.ch + pos - 1; i < len; ++i, ++c) { //复制S的子串到Sub
Sub->ch[i] = *c;
}
Sub->length = len;
}
}
return OK;
}
int Index(HString S, HString T, int pos) {
int i = pos, j = 1;
while (i <= S.length && j <= T.length) {
if (S.ch[i - 1] == T.ch[j - 1]) {
++i;
++j;
} else {
i = i - j + 2;
j = 1;
}
}
if (j > T.length) {
return i - T.length;
} else {
return 0;
}
}
Status Replace(HString *S, HString T, HString V) {
int i = 1;
while (i) {
i = Index(*S, T, i);
if (i) {
StrDelete(S, i, StrLength(T));
StrInsert(S, i, V);
i += StrLength(V);
}
}
return OK;
}
Status StrInsert(HString *S, int pos, HString T) {
int i;
char *c;
if (pos < 1 || pos > S->length + 1) { //参数不合法
return ERROR;
}
if (!(S->ch = (char *) realloc(S->ch, (S->length + T.length) * sizeof(char)))) { //分配空间失败
return ERROR;
} else {
for (i = S->length - 1, c = S->ch + S->length + T.length - 1; i >= pos - 1; --i, --c) { //将pos及之后的字符后移T.length个位置
*(c) = S->ch[i];
}
for (i = 0, c = T.ch; i < T.length; ++i, ++c) { //复制T到S的pos位置
S->ch[pos + i - 1] = *c;
}
S->length += T.length;
}
return OK;
}
Status StrDelete(HString *S, int pos, int len) {
int i;
char *c;
if (pos < 1 || pos > S->length || len < 0 || pos + len - 1 > S->length) { //参数不合法
return ERROR;
}
for (i = pos + len - 1, c = S->ch + i; i < S->length; ++i, ++c) { //将pos+len及之后的字符前移len个位置
S->ch[i - len] = *c;
}
S->length -= len;
S->ch = (char *) realloc(S->ch, S->length * sizeof(char)); //释放多余空间
return OK;
}
void StrPrint(HString S) {
int i;
for (i = 0; i < S.length; ++i) {
printf("%c", S.ch[i]);
}
printf("\n");
}
int main() {
HString S, T, Sub, V;
char *chars = "hello world";
char *sub_chars = "world";
char *v_chars = "C++";
int pos = 7;
int len = 5;
StrAssign(&S, chars);
StrAssign(&T, "abc");
StrAssign(&Sub, sub_chars);
StrAssign(&V, v_chars);
printf("S: ");
StrPrint(S);
printf("T: ");
StrPrint(T);
printf("Sub: ");
StrPrint(Sub);
printf("V: ");
StrPrint(V);
printf("StrCopy(&S, T): %d\n", StrCopy(&S, T));
printf("S: ");
StrPrint(S);
printf("StrCompare(S, T): %d\n", StrCompare(S, T));
printf("StrLength(S): %d\n", StrLength(S));
printf("ClearString(&S): %d\n", ClearString(&S));
printf("S: ");
StrPrint(S);
printf("Concat(&S, T, Sub): %d\n", Concat(&S, T, Sub));
printf("S: ");
StrPrint(S);
printf("SubString(&Sub, S, %d, %d): %d\n", pos, len, SubString(&Sub, S, pos, len));
printf("Sub: ");
StrPrint(Sub);
printf("Index(S, Sub, %d): %d\n", pos, Index(S, Sub, pos));
printf("Replace(&S, Sub, V): %d\n", Replace(&S, Sub, V));
printf("S: ");
StrPrint(S);
printf("StrInsert(&S, %d, V): %d\n", pos, StrInsert(&S, pos, V));
printf("S: ");
StrPrint(S);
printf("StrDelete(&S, %d, %d): %d\n", pos, len, StrDelete(&S, pos, len));
printf("S: ");
StrPrint(S);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)