采用堆分配存储表示的串被称为堆串,与顺序串相比,地址空间仍是连续,但空间是在程序执行时动态分配的。
程序中会使用到的realloc函数:
//realloc : (void *)reelloc (void *ptr,unsigned newsize);
//使用 : char *str;
// str = (char *)realloc(str,20);
代码实现如下:
<span style="font-size:18px;">#pragma once
#include<iostream>
using namespace std;
#define MAX_SZIE 30
typedef struct
{
char *str;
int length;
}HeapString;
/*判断空*/
bool IsEmpty(HeapString *s);
/*初始化*/
void InintString(HeapString *s);
/*求长度*/
int Length(HeapString *s);
/*用字符串给串赋值*/
bool StrAssign(HeapString *s, char cstr[]);
/*打印*/
void StrPrint(HeapString *s);
/*在S的pos位置插入串T*/
bool StrInsert(HeapString *S, int pos, HeapString *T);
/*在S的pos位置删除长度为len的子串*/
bool StrDelete(HeapString *S, int pos, int len);
/*清空串*/
void ClearString(HeapString *s);
/*摧毁*/
void Destroy(HeapString *s);
/*从S的pos位置截取长为len的子串sub*/
bool SubString(HeapString *sub, HeapString *s, int pos, int len);
/*拼接*/
bool StringConcat(HeapString *T, HeapString *S, HeapString *S2);
/*拷贝*/
bool StrCopy(HeapString *T, HeapString *S);
/*比较*/
int StrCompare(HeapString *T, HeapString *S)