串的链式存储说明
串的块链存储表示和线性表的链式存储结构相类似,也可采用链表方式存储串值。由于串结构的特殊性——结构中的每个数据元素是一个字符,则用链表存储串值时,存在一个“结点大小”的问题,即每个结点可以存放一个字符,也可以存放多个
字符。当结点大小大于1时,由于串长不一定是结点大小的整数倍,则链表中的最后一个结点不一定被串值占满,此时通常补上“#”或其他的非串值字符(通常“#”不属于串的字符集,是一个特殊的符号)。
为了便于进行串的操作,当以链表存储串值时,除头指针外还可附设一个尾指针指示链表中的最后一个结点,并给出当前串的长度。称如此定义的串存储结构为块链结构,说明如下:
== == == 串的块链存储表示 == == ==
#define CHUNKSIZE 80 //可由用户定义的块大小
typedef struct Chunk{
char ch[CHUNKSIZE];
struct Chunk *next;
}Chunk;
typedef struct{
Chunk *head,*tail; //串的头和尾指针
int curlen; //串的当前长度
}LString;
全部代码
#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
#include "string.h"
#define CHUNKSIZE 4 //链表中一个结点所含字符的最大值
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OVERFLOW -2
typedef struct Chunk {
char ch[CHUNKSIZE];
struct Chunk* next;
}Chunk;
typedef struct {
Chunk* head, * tail;
int length;
}LString;
void InitString(LString* S);
int StrAssign(LString* S, char* cstr);
int StrEmpty(LString S);
int StrLength(LString S);
int StrCopy(LString* T, LString S);
int StrCompare(LString S, LString T);
int ClearString(LString* S);
int Concat(LString *T,LString S1,LString S2);
int SubString(LString *Sub,LString S,int pos,int len);
int Index(LString S, LString T, int pos);
int StrInsert(LString* S, int pos, LString T);
int StrDelete(LString* S, int pos, int len);
int Replace(LString* S, LString T, LString V);
void Print(LString S);
void NodePut(Chunk c);
int main() {
char c[200];
int i, j;
LString LS, LT, LV;
InitString(&LS);
InitString(<);
InitString(&LV);
printf("输入串LS:\n");
gets(c);
StrAssign(&LS, c);
printf("串LS为:");
Print(LS);
printf("串LS的长度为:%d\n",StrLength(LS));
printf("输入串LT:\n");
gets(c);
StrAssign(<, c);
printf("串LT为:");
Print(LT);
printf("串LT的长度为:%d\n", StrLength(LT));
//== == == == == == == == == == == == == == == == == ==
/*printf("串插入:\n");
printf("输入插入位置:");
scanf_s("%d", &i);
StrInsert(&LS,i,LT);
Print(LS);
printf("LS的长度:%d\n",LS.length);*/
/*Concat(&LV, LS, LT);
Print(LV);*/
/*printf("删除测试LS:\n");
printf("输入删除位置和删除长度(,隔开):");
scanf_s("%d,%d",&i,&j);
StrDelete(&LS, i, j);
printf("LS:");
Print(LS);
printf("删除测试后的LS长度:%d", LS.length);*/
/*printf("替换测试:\n");
printf("LS为主串,LT为被替换串,请输入替换串LV:\n");
printf("LV: ");
gets(c);
StrAssign(&LV, c);
Replace(&LS, LT, LV);
printf("\n");
Print(LS);*/
}
void InitString(LString* S) { //初始化字符串
S->head = NULL;
S->tail = NULL;
S->length = 0;
}
int StrAssign(LString* S, char *cstr) {
//初始条件:cstr是字符串常量;操作结果:生成一个其值等于cstr的串T。
Chunk* p;
char* q;
q = cstr;
int len = 0;
while (*q) {
len++;
q++;
}
if (len) S->length = len;
else return ERROR;
while (len>0) {
len = len - CHUNKSIZE;
char ch[CHUNKSIZE];
p = (Chunk*)malloc(sizeof(Chunk));
if (!p)return ERROR;
for (int i = 0; i < CHUNKSIZE; i++) {
if (!*cstr) {
p->ch[i] = '#';
}
else {
p->ch[i] = *cstr++;
}
}
p->next = NULL;
if (S->head == NULL) {
S->head = p;
S->head->next = NULL;
S->tail = S->head;
}
else {
S->tail->next = p;
S->tail = p;
}
}
return OK;
}
int StrEmpty(LString S) {
//初始条件:串S存在;操作结果:若S为空串,则返回TRUE,否则返回FALSE。
if (S.length==0)return TRUE;
else return FALSE;
}
int StrLength(LString S) {
//初始条件:串S存在。操作结果:返回S的元素个数,称为串的长度。
return S.length;
}
int StrCopy(LString* T, LString S) {
//初始条件:串S存在。操作结果:由串S复制得串T。
Chunk* p;
InitString(T); //初始化后避免了在原串T后面直接添加串S
T->length = S.length;
while (S.head != NULL) {
p = (Chunk*)malloc(sizeof(Chunk));
if (!p)return ERROR;
p->next = NULL;
for (int i = 0; i < CHUNKSIZE; i++)
p->ch[i] = S.head->ch[i];
//上面为生成数据结点,下面为插入数据结点
if (T->head == NULL)T->head = T->tail = p;
else {
T->tail->next = p;
T->tail = p;
}
S.head = S.head->next;//循环
}
return OK;
}
int StrCompare(LString S, LString T) {
//初始条件:串S和T存在。操作结果:若S>T,则返回值>0;若S=T,则返回值=0;若S<T,则返回值<0。
if (!S.head || !T.head) {
printf("需要比较的串有误!\n");
exit(OVERFLOW);
}
if (S.length != T.length)return (S.length-T.length);
while (S.head != NULL) {
for (int i = 0; i < CHUNKSIZE; i++) {
if (S.head->ch[i] == T.head->ch[i])
continue;
else return (S.head->ch[i] - T.head->ch[i]);
}
S.head = S.head->next;
T.head = T.head->next;
}
return 0;
}
int ClearString(LString* S) {
//初始条件:串S存在。操作结果:将S清为空串。
if (!S->head)return ERROR;
Chunk* p;
while (S->head) {
p = S->head;
S->head = S->head->next;
free(p);
}
S->head = NULL;
S->tail = NULL;
S->length = 0;
return OK;
}
int Concat(LString* T, LString S1, LString S2) {
//初始条件:串S1和S2存在。操作结果:用T返回由S1和S2联接而成的新串。
Chunk* p, * q=NULL;
if (!S1.head && !S2.head)return ERROR;
if (T->head != T->tail)ClearString(T);
if (StrEmpty(S1)) {
StrCopy(T, S2);
return OK;
}
StrCopy(T, S1);
T->length = S1.length + S2.length;
while (S2.head) {
//创造新结点,将S2头结点(第一个数据结点)的数据赋给新结点
p = (Chunk*)malloc(sizeof(Chunk));
if (!p)return ERROR;
else {
for (int g = 0; g < CHUNKSIZE; g++) {//为新结点赋值S2的头结点
p->ch[g] = S2.head->ch[g];
}
p->next = NULL;
}
//将T的尾结点中'#'依序替换为新结点的值,并将新结点中被替换的值改为'#'。
int n = 0;
for (int k = 0; k < CHUNKSIZE; k++) {
if (T->tail->ch[k] == '#') {
for (n; n < CHUNKSIZE; n++) {
if (p->ch[n] != '#') {
T->tail->ch[k] = p->ch[n];
p->ch[n] = '#';
break;
}
}
}
}
//处理新结点p,数据在前,#号在后
for (int ind = 0; ind < CHUNKSIZE; ind++) {
if (p->ch[ind] == '#') {
for (int indl = ind + 1; indl < CHUNKSIZE; indl++) {
if (p->ch[indl] != '#') {
p->ch[ind] = p->ch[indl];
p->ch[indl] = '#';
break;
}
}
}
}
//将p链接到T尾部,修改T的尾结点
q = T->tail;
T->tail->next = p;
T->tail = p;
if (T->tail->ch[0] == '#'&&T->tail->ch[1]=='#' && T->tail->ch[2] == '#' && T->tail->ch[3] == '#') {
T->tail = q;
T->tail->next = NULL;
}
//循环S2的头结点
S2.head = S2.head->next;
}
return OK;
}
int SubString(LString* Sub, LString S, int pos, int len) {
//初始条件:串S存在,1≤pos≤StrLength(S)且0≤len≤StrLength(S)-pos+1。
//操作结果:用Sub返回串S的第pos个字符起长度为len的子串。
int j = 0, cnt = 0;
char ch[200];
if (Sub->head != Sub->tail)ClearString(Sub);
InitString(Sub);
if (!S.head||len>200)return ERROR;
if (pos<1 || pos>S.length || len<0 || len>(StrLength(S) - pos + 1))
return FALSE;
Sub->length = len;
if (len == 0) return OK;
while (S.head) {
for (int i = 0; i < CHUNKSIZE; i++) {
cnt++; //数数,cnt代表位置,并非数组下标。
if (pos == cnt||cnt>pos) {
ch[j] = S.head->ch[i];
j++;
}
if (cnt - pos == len - 1)break; //跳出里层循环
}
if (cnt - pos == len - 1)break; //跳出外层循环
S.head = S.head->next;
}
ch[j] = '\0'; //在字符数组末尾添加'\0'(代表字符数组可转换为字符串),方便程序读取和输出
StrAssign(Sub, ch);
return OK;
}
int Index(LString S, LString T, int pos) {
//初始条件:串S和T存在,T是非空串,1≤pos≤StrLength(S)。
//操作结果:若主串S中存在和串T值相同的子串,则返回它在主串S中第pos个字符之后第一次出现的位置;否则函数值为0。
int i;
LString Sub;
InitString(&Sub);
i = pos;
if (pos > 0) {
while (i<=S.length-T.length+1) {
SubString(&Sub, S, i, T.length);
if (StrCompare(Sub, T) != 0)++i;
else return i; //返回子串在主串中的位置
}
}
return 0; //S中不存在与T相等的子串
}
int StrInsert(LString* S, int pos, LString T) {
//初始条件:串S和T存在,1≤pos≤StrLength(S)+1。操作结果:在串S的第pos个字符之前插入串T。
LString pos_for, pos_lat, new;
if (!S->head || !T.head || pos<1 || pos>StrLength(*S) + 1)return ERROR;
SubString(&pos_for, *S, 1, pos - 1);
SubString(&pos_lat, *S, pos, StrLength(*S) - pos + 1);
Concat(S, pos_for, T);
Concat(&new, *S, pos_lat);
StrCopy(S, new);
return OK;
}
int StrDelete(LString* S, int pos, int len) {
//初始条件:串S存在,1≤pos≤StrLength(S)-len+1。操作结果:从串S中删除第pos个字符起长度为len的子串。
LString pos_for, pos_lat;
if (!S->head || pos<1 || pos>StrLength(*S) - len + 1)return ERROR;
SubString(&pos_for, *S, 1, pos - 1);
SubString(&pos_lat, *S, pos + len, S->length-pos-len+1);
Concat(S, pos_for, pos_lat);
return OK;
}
int Replace(LString* S, LString T, LString V) {
//初始条件:串S,T和V存在,T是非空串。操作结果:用V替换主串S中出现的所有与T相等的不重叠的子串。
int i = 1;
if (!S->head || StrEmpty(T) || !V.head)return ERROR;
while (i) {
i = Index(*S, T, i);
if (i == 0)break;
StrDelete(S, i, T.length);
StrInsert(S, i, V);
i = i + V.length;
}
return OK;
}
void Print(LString S) {
//输出字符串
while (S.head != NULL) {
for (int i = 0; i < CHUNKSIZE; i++) {
if (*(S.head->ch + i) == '#')break;
printf("%c",*(S.head->ch+i));
}
S.head = S.head->next;
}
printf("\n");
}
void NodePut(Chunk c) {
//结点输出
for (int i = 0; i < CHUNKSIZE; i++)
printf("%c", c.ch[i]);
printf("\n");
}
写在后面:代码稍微有点长,有许多需要改善的地方,有其他问题留言相互交流学习;