基本概念
串(String)是由零个或多个字符组成的有限序列。它是线性表的特殊形式,其数据元素被限制为字符集。
抽象数据类型(ADT)
串的数据对象约束为字符集,数据关系与线性表类似:除第一个和最后一个元素外,其余元素有且仅有一个直接前驱和直接后驱。
主要基本操作包括:
StrAssign(*T, chars):生成一个值等于 chars 的串 T。StrCopy(*T, S):由串 S 复制得到串 T。StrEmpty(S):若串 S 为空串返回 TRUE,否则返回 FALSE。StrCompare(S, T):比较大小,S>T 返回 1,S=T 返回 0,S<T 返回 -1。StrLength(S):返回串 S 的元素个数。ClearString(*S):清空串 S。Contact(*T, S1, S2):用 T 返回由 S1 和 S2 联接而成的新串。SubString(Sub, S, pos, len):获取 S 中从第 pos 个字符起长度为 len 的子串。Index(S, T, pos):查找主串 S 中是否存在与 T 相同的子串,返回位置或 0。Replace(*S, T, V):用 V 替换 S 中所有与 T 相等的不重叠子串。StrInsert(*S, pos, T):在 S 的第 pos 个字符之前插入串 T。StrDelete(*S, pos, len):删除 S 中从第 pos 个字符起长度为 len 的子串。
顺序存储
串的顺序存储方式通常有两种:定长数组和堆分配存储。前者因数组长度固定,容易出现溢出问题;后者通过动态内存管理更为灵活。这里重点介绍基于堆分配的存储实现。
我们定义一个结构体 HString,包含指向存储基地址的指针 ch 和记录长度的 length。为了安全起见,申请空间时会多预留一个字节用于存放结束符 \0。
#include <stdlib.h>
#include <stdio.h>
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100
typedef struct {
char* ch; // 串的存储基地址
int length; // 串的长度
} HString;
// 新建串
int StrAssign(HString* T, char* chars) {
int i = 0;
char* c = chars;
if (T->ch) free(T->ch);
while (*c) {
c++;
i++;
}
T->length = i;
if (i == 0) {
T->ch = NULL;
} else {
if (!(T->ch = (char*)malloc(sizeof(char)*(i+1)))) return ERROR;
for (i = 0; i < T->length; i++) {
T->ch[i] = chars[i];
}
T->ch[i] = '\0';
}
return OK;
}
// 复制串
int StrCopy(HString* T, char* S) {
int i = 0, j;
char* c = S;
while (*c) {
c++;
i++;
}
if (T->ch) free(T->ch);
if (!(T->ch = (char*)malloc(sizeof(char)*i + 1))) return ERROR;
for (j = 0; j < i; j++) {
T->ch[j] = S[j];
}
T->length = i;
T->ch[j] = '\0';
return OK;
}
// 验证串是否为空
int StrEmpty(HString S) {
if (S.length == 0) return TRUE;
return FALSE;
}
// 返回串长度
int StrLength(HString S) {
return S.length;
}
// 清空串
int ClearString(HString* S) {
if (S->ch) {
free(S->ch);
S->ch = NULL;
}
S->length = 0;
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 1;
if (S.ch[i] < T.ch[i]) return -1;
}
if (S.length > T.length) return 1;
else if (S.length < T.length) return -1;
else return 0;
}
// 连接两个串
int Contact(HString* T, HString S1, HString S2) {
int i, j;
if (T->ch) free(T->ch);
if (!(T->ch = (char*)malloc(sizeof(char)*(S1.length + S2.length + 1)))) return ERROR;
for (i = 0; i < S1.length; i++) T->ch[i] = S1.ch[i];
for (j = 0; j < S2.length; j++) T->ch[i+j] = S2.ch[j];
T->ch[i+j] = '\0';
T->length = S1.length + S2.length;
return OK;
}
// 取子串
int 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) free(Sub->ch);
if (!(Sub->ch = (char*)malloc(sizeof(char)*(len+1)))) return ERROR;
for (i = 0; i < len; i++) {
Sub->ch[i] = S.ch[pos+i-1];
}
Sub->ch[i] = '\0';
Sub->length = len;
return OK;
}
// 查找子串位置
int Index(HString S, HString T, int pos) {
int i, j;
if (pos < 1 || pos > S.length || T.length == 0) return ERROR;
if (T.length > S.length) return FALSE;
j = pos;
do {
for (i = 0; i < T.length; i++) {
if (S.ch[j+i-1] != T.ch[i]) {
j++;
break;
}
}
} while (i < T.length && j < S.length);
if (i >= T.length) return j - pos + 1;
else return FALSE;
}
// 替换子串
int Replace(HString* S, HString T, HString V) {
int i, j = 1;
HString s, Sub, str, S1;
s.ch = NULL; Sub.ch = NULL; str.ch = NULL; S1.ch = NULL;
s.length = 0; Sub.length = 0; str.length = 0; S1.length = 0;
if (S->length == 0 || T.length == 0) return ERROR;
i = Index(*S, T, j);
if (i == 0) return FALSE;
while (i != 0) {
SubString(&Sub, *S, j, i - 1);
Contact(&s, Sub, V);
Contact(&str, S1, s);
StrCopy(&S1, str.ch);
j += i + T.length - 1;
i = Index(*S, T, j);
}
if (j <= S->length) {
SubString(&Sub, *S, j, S->length - j + 1);
Contact(&str, S1, Sub);
StrCopy(&S1, str.ch);
}
*S = S1;
return OK;
}
// 插入子串
int StrInsert(HString* S, int pos, HString T) {
HString Sub1, S1, Sub2;
Sub1.ch = NULL; S1.ch = NULL; Sub2.ch = NULL;
Sub1.length = 0; S1.length = 0; Sub2.length = 0;
if (pos < 1 || pos > S->length + 1 || T.length == 0) return ERROR;
SubString(&Sub1, *S, 1, pos - 1);
Contact(&S1, Sub1, T);
SubString(&Sub2, *S, pos, S->length - pos + 1);
Contact(S, S1, Sub2);
return OK;
}
// 删除子串
int StrDelete(HString* S, int pos, int len) {
HString Sub1, Sub2;
Sub1.ch = NULL; Sub2.ch = NULL;
Sub1.length = 0; Sub2.length = 0;
if (pos < 1 || pos > S->length || len > S->length - pos + 1) return ERROR;
SubString(&Sub1, *S, 1, pos - 1);
SubString(&Sub2, *S, pos + len, S->length - pos - len + 1);
Contact(S, Sub1, Sub2);
return OK;
}
int main() {
HString S, T, Sub, V, S1, S2;
S.ch = NULL; T.ch = NULL; Sub.ch = NULL; V.ch = NULL; S1.ch = NULL; S2.ch = NULL;
char s[MAXSIZE];
int pos, choice, len;
do {
printf("输入操作:0.退出 1.返回串长度 2.插入串 3.删除串 4.获取子串 5.清空串 6.创建串 7.查找串 8.验证串是否为空 9.替换串 10.连接串 11.比较串 12.复制串\n");
scanf("%d", &choice);
switch (choice) {
case 1: { printf("%d\n", StrLength(S)); break; }
case 2: {
printf("输入插入的位置\n"); scanf("%d", &pos);
printf("输入插入的串\n"); scanf("%s", s);
StrAssign(&T, s); StrInsert(&S, pos, T);
printf("S=%s\n", S.ch); break;
}
case 3: {
printf("输入删除的位置\n"); scanf("%d", &pos);
printf("输入删除的长度\n"); scanf("%d", &len);
StrDelete(S, pos, len); printf("S=%s\n", S.ch); break;
}
case 4: {
printf("输入子串的起始位置\n"); scanf("%d", &pos);
printf("输入子串的长度\n"); scanf("%d", &len);
SubString(&Sub, S, pos, len); printf("Sub=%s\n", Sub.ch); break;
}
case 5: { ClearString(&S); printf("S=%s\n", S.ch); break; }
case 6: {
printf("输入串的内容\n"); scanf("%s", s);
StrAssign(&S, s); printf("S=%s\n", S.ch); break;
}
case 7: {
printf("输入在主串中的起始查找位置\n"); scanf("%d", &pos);
printf("输入查找的子串\n"); scanf("%s", s);
StrAssign(&T, s); printf("Index=%d\n", Index(S, T, pos)); break;
}
case 8: { printf("%d\n", StrEmpty(S)); break; }
case 9: {
printf("输入被替换的子串\n"); scanf("%s", s); StrAssign(&T, s);
printf("输入替换的子串\n"); scanf("%s", s); StrAssign(&V, s);
Replace(&S, T, V); printf("S=%s\n", S.ch); break;
}
case 10: {
printf("输入第一个子串\n"); scanf("%s", s); StrAssign(&S1, s);
printf("输入第二个的子串\n"); scanf("%s", s); StrAssign(&S2, s);
Contact(&T, S1, S2); printf("T=%s\n", T.ch); break;
}
case 11: {
printf("输入第一个子串\n"); scanf("%s", s); StrAssign(&S1, s);
printf("输入第二个的子串\n"); scanf("%s", s); StrAssign(&S2, s);
printf("result=%d\n", StrCompare(S1, S2)); break;
}
case 12: {
printf("输入要复制的串\n"); scanf("%s", s);
StrCopy(&S, s); printf("S=%s\n", S.ch); break;
}
}
} while (choice != 0);
return 0;
}
链式存储
除了顺序存储,串也可以使用链式存储。每个结点可以存放多个字符以提高空间利用率。链表尾结点不一定全满,通常不填充非串值字符。为方便连接操作,除头指针外还可附设尾指针。虽然链式存储避免了顺序存储的溢出问题,但在随机访问和整体操作上不如顺序存储灵活。
