串的堆分配C语言代码
时间: 2023-11-29 16:45:02 浏览: 67
以下是串的堆分配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) {
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);
}
T->ch = (char *) malloc(S.length * sizeof(char)); //分配存储空间
if (!T->ch) { //分配存储空间失败
return ERROR;
} else { //分配存储空间成功
for (i = 0; i < S.length; ++i) {
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;
if (T->ch) { //如果T串存在,则释放T串的空间
free(T->ch);
}
T->ch = (char *) malloc((S1.length + S2.length) * sizeof(char)); //分配存储空间
if (!T->ch) { //分配存储空间失败
return ERROR;
} else { //分配存储空间成功
for (i = 0; i < S1.length; ++i) {
T->ch[i] = S1.ch[i];
}
for (i = 0; i < S2.length; ++i) {
T->ch[S1.length + i] = S2.ch[i];
}
T->length = S1.length + S2.length;
}
return OK;
}
Status SubString(HString *Sub, HString S, int pos, int len) {
int i;
if (pos < 1 || pos > S.length || len < 0 || len > S.length - pos + 1) { //参数不合法
return ERROR;
}
if (Sub->ch) { //如果Sub串存在,则释放Sub串的空间
free(Sub->ch);
}
if (!len) { //如果子串长度为0
Sub->ch = NULL;
Sub->length = 0;
} else { //子串长度不为0
Sub->ch = (char *) malloc(len * sizeof(char)); //分配存储空间
if (!Sub->ch) { //分配存储空间失败
return ERROR;
} else { //分配存储空间成功
for (i = 0; i < len; ++i) {
Sub->ch[i] = S.ch[pos - 1 + i];
}
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;
if (pos < 1 || pos > S->length + 1) { //参数不合法
return ERROR;
}
if (T.length) { //插入非空串
S->ch = (char *) realloc(S->ch, (S->length + T.length) * sizeof(char)); //重新分配存储空间
if (!S->ch) { //分配存储空间失败
return ERROR;
} else { //分配存储空间成功
for (i = S->length - 1; i >= pos - 1; --i) {
S->ch[i + T.length] = S->ch[i];
}
for (i = 0; i < T.length; ++i) {
S->ch[pos - 1 + i] = T.ch[i];
}
S->length += T.length;
}
}
return OK;
}
Status StrDelete(HString *S, int pos, int len) {
int i;
if (pos < 1 || pos > S->length || len < 0 || len > S->length - pos + 1) { //参数不合法
return ERROR;
}
if (len) { //删除非空串
for (i = pos - 1; i < S->length - len; ++i) {
S->ch[i] = S->ch[i + len];
}
S->ch = (char *) realloc(S->ch, (S->length - len) * sizeof(char)); //重新分配存储空间
if (!S->ch) { //分配存储空间失败
return ERROR;
} else { //分配存储空间成功
S->length -= len;
}
}
return OK;
}
```
阅读全文