typedef struct snode {
char data;
struct snode* next;
}LinkStrNode;
生成串
void StrAssign(LinkStrNode*& s, char cstr[])
{
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
LinkStrNode* p, * r;
r = s;
for (int i = 0; cstr[i] != '\0'; i++)
{
p = (LinkStrNode*)malloc(sizeof(LinkStrNode));
p->data = cstr[i];
r->next = p;
r = p;
}
r->next = NULL;
}
销毁串
void DestroyStr(LinkStrNode*& s)
{
LinkStrNode* pre, * p;
pre = s;
p = s->next;
while (p != NULL)
{
free(pre);
pre = p;
p = pre->next;
}
free(pre);
}
串的复制
void StrCopy(LinkStrNode*& s, LinkStrNode* t)
{
LinkStrNode* p, * q, * r;
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
r = s;
p = t->next;
while (p != NULL)
{
q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
q->data = p->data;
r->next = q;
r = q;
p = p->next;
}
r->next = NULL;
}
判断串相等
bool StrEqual(LinkStrNode* s, LinkStrNode* t)
{
LinkStrNode* p, * q;
p = s->next;
q = t->next;
while (p != NULL && q != NULL && p->data == q->data)
{
p = p->next;
q = q->next;
}
if (p == NULL && q == NULL)
return true;
else
return false;
}
求串长
int StrLenhth(LinkStrNode* s)
{
int i = 0;
LinkStrNode* p = s->next;
while (p != NULL)
{
i++;
p = p->next;
}
return i;
}
串连接
LinkStrNode* ConStr(LinkStrNode* ls, LinkStrNode* t)
{
LinkStrNode* p, * q, * s, * r;
LinkStrNode* str = (LinkStrNode*)malloc(sizeof(LinkStrNode));
p = ls->next;
q = t->next;
r = str;
while (p != NULL)
{
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
s->data = p->data;
r->next = s;
r = s;
p = p->next;
}
while (q != NULL)
{
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
s->data = q->data;
r->next = s;
r = s;
q = q->next;
}
r->next = NULL;
return str;
}
求子串
LinkStrNode* SubStr(LinkStrNode* ls, int i, int j)
{
int k ;
LinkStrNode* str, * s, * r, * p;
str = (LinkStrNode*)malloc(sizeof(LinkStrNode));
str->next = NULL;
r = str;
p = ls->next;
if (i <= 0 || i > StrLenhth(ls) || j < 0 || i + j - 1 > StrLenhth(ls))
return str;
else
{
for (k = 1; k < i; k++)
{
p = p->next;
}
for (k = 1; k <= j; k++)
{
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
s->data = p->data;
r->next = s;
r = s;
p = p->next;
}
r->next = NULL;
return str;
}
}
子串插入
LinkStrNode* InsStr(LinkStrNode* ls, int i, LinkStrNode* t)
{
LinkStrNode* str, * p, *q, *s, * r;
str = (LinkStrNode*)malloc(sizeof(LinkStrNode));
str->next = NULL;
r = str;
p = ls->next;
q = t->next;
if (i <= 0 || i > StrLenhth(ls) + 1)
return str;
else
{
for (int j = 1; j < i; j++)
{
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
s->data = p->data;
r->next = s;
r = s;
p = p->next;
}
while (q != NULL)
{
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
s->data = q->data;
r->next =s;
r = s;
q = q->next;
}
while (p != NULL)
{
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
s->data = p->data;
r->next = s;
r = s;
p = p->next;
}
r->next = NULL;
return str;
}
}
子串删除
LinkStrNode* DelStr(LinkStrNode* ls, int i, int j)
{
LinkStrNode* str, * s, * r, * p;
int k;
str = (LinkStrNode*)malloc(sizeof(LinkStrNode));
str->next = NULL;
r = str;
p = ls->next;
if (i <= 0 || i > StrLenhth(ls) || j < 0 || i + j - 1>StrLenhth(ls))
return str;
for (k = 1; k < i; k++)
{
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
s->data = p->data;
r->next = s;
r = s;
p = p->next;
}
for (k = 1; k <= j; k++)
{
p = p->next;
}
while (p != NULL)
{
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
s->data = p->data;
r->next = s;
r = s;
p = p->next;
}
r->next = NULL;
return str;
}
子串替换
LinkStrNode* DepStr(LinkStrNode* ls, int i, int j, LinkStrNode* t)
{
int k;
LinkStrNode* str, * p, * q, * s, * r;
str = (LinkStrNode*)malloc(sizeof(LinkStrNode));
str->next = NULL;
r = str;
p = ls->next;
q = t->next;
if (i <= 0 || i > StrLenhth(ls) || j<0 || i + j - 1>StrLenhth(ls))
return str;
else
{
for (k = 1; k < i; k++)
{
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
s->data = p->data;
r->next = s;
r = s;
p = p->next;
}
while (q != NULL)
{
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
s->data = q->data;
r->next = s;
r = s;
q = q->next;
}
for (k = 1; k <= j; k++)
{
p = p->next;
}
while (p != NULL)
{
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
s->data = p->data;
r->next = s;
r = s;
p = p->next;
}
r->next = NULL;
return str;
}
}
输出串
void DispStr(LinkStrNode* s)
{
LinkStrNode* p = s->next;
while (p != NULL)
{
printf("%c", p->data);
p = p->next;
}
printf("\n");
}
把串中最先出现的"ab"改为”xyz"
void Rep(LinkStrNode*& ls)
{
LinkStrNode* pre, * p, * s;
pre = ls->next;
p = pre->next;
while (pre != NULL && p != NULL)
{
if (pre->data == 'a' && p->data == 'b')
{
pre->data = 'x';
p->data = 'z';
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
s->data = 'y';
pre->next = s;
s->next = p;
break;
}
pre = pre->next;
p = p->next;
}
}