赞
踩
数据结构-串(第四章)的整理笔记,若有错误,欢迎指正。
数据结构—顺序串 Ⅵ
串
的
基
本
操
作
{
S
t
r
A
s
s
i
g
n
(
&
s
,
c
s
t
r
)
—
生
成
串
D
e
s
t
r
o
y
S
t
r
(
&
s
)
—
销
毁
串
S
t
r
C
o
p
y
(
&
s
,
t
)
—
串
的
复
制
S
t
r
E
q
u
a
l
(
s
,
t
)
—
判
断
串
相
等
S
t
r
L
e
n
g
t
h
(
s
)
—
求
串
长
C
o
n
c
a
t
(
s
,
t
)
—
串
的
连
接
S
u
b
S
t
r
(
s
,
i
,
j
)
—
求
子
串
I
n
s
S
t
r
(
s
1
,
i
,
s
2
)
—
子
串
的
插
入
D
e
l
S
t
r
(
s
,
i
,
j
)
—
子
串
的
删
除
R
e
p
S
t
r
(
s
,
i
,
j
,
t
)
—
子
串
的
替
换
D
i
s
p
S
t
r
(
s
)
—
输
出
串
串的基本操作
typedef struct
{
char data[4]; //存放字符
LinkStrNode* next; //指向下一个结点的指针
}LinkStrNode; //链串的结点类型
typedef struct
{
char data; //存放字符
LinkStrNode* next; //指向下一个结点的指针
}LinkStrNode; //链串的结点类型
void StrAssign(LinkStrNode*& s, char cstr[])
{
int i;
LinkStrNode* r, *p;
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
r = s; //r始终指向尾结点
for (i = 0; cstr[i] != '\0'; i++)
{
p = (LinkStrNode*)malloc(sizeof(LinkStrNode));
p->data = cstr[i];
r->next = p;
r = p;
}
r->next = NULL; //尾结点的next域置为空
}
void Destroy(LinkStrNode*& s)
{
LinkStrNode* pre = s, * p = s->next; //pre指向结点p的前驱节点
while (p != NULL) //扫描链串s
{
free(pre); //释放pre结点
pre = p; //pre、p同步后移一个结点
p = p->next;
}
free(pre); //循环结束时p为NULL,pre指向尾结点,释放它
}
void StrCopy(LinkStrNode*& s, LinkStrNode* t)
{
LinkStrNode* p = t->next, * q, * r;
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
r = s; //r始终指向尾结点
while (p != NULL) //扫描链串t的所有结点
{
q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
q->data = p->data; //将p结点复制到q结点
r->next = q; //q结点链接到链串s的末尾
r = q;
p = p->next;
}
r->next = NULL; //尾结点的next域置为空
}
bool StrEqual(LinkStrNode* s, LinkStrNode* t)
{
LinkStrNode* p = s->next, * q = t->next; //p、q分别扫描链串s和t的数据结点
while (p != NULL && q != NULL && p->data == q->data)
{
p = p->next;
q = q->next;
}
if (p == NULL && q == NULL) return true; //s和t的长度相等且对应位置的字符均相同
else return false;
}
int StrLength(LinkStrNode* s)
{
int i = 0; //i用于累计数据结点的个数
LinkStrNode* p = s->next; //p指向链串s的首结点
while (p != NULL) //扫描所有数据结点
{
i++;
p = p->next;
}
return i;
}
LinkStrNode* Concat(LinkStrNode* s, LinkStrNode* t) { LinkStrNode* str, * p = s->next, * q, * r; str = (LinkStrNode*)malloc(sizeof(LinkStrNode)); r = str; //r指向结果串的尾结点 while(p!=NULL) //用p扫描s的所有数据结点 { q = (LinkStrNode*)malloc(sizeof(LinkStrNode)); q->data = p->data; //将p结点复制到q结点中 r->next = p; //将q结点链接到str的末尾 r = p; p = p->next; } p = t->next; while (p != NULL) //用p扫描t的所有数据结点 { q = (LinkStrNode*)malloc(sizeof(LinkStrNode)); q->data = p->data; //将p结点复制到q结点中 r->next = p; //将q结点链接到str的末尾 r = p; p = p->next; } r->next = NULL; //尾结点的next域置为空 return str; }
LinkStrNode* SubStr(LinkStrNode* s, int i, int j) { int k; LinkStrNode* str, * p = s->next, * q, * r; str = (LinkStrNode*)malloc(sizeof(LinkStrNode)); str->next = NULL; //置结果串str为空串 r = str; //r指向结果串的尾结点 if (i <= 0 || i > StrLength(s) || j<0 || i + j - 1>StrLength(s)) return str; //参数不正确时返回空串 for (k = 0; k < i; k++) p = p->next; //让p指向链串s的第i个数据结点 for (k = 1; k <= j; k++) //将s的从第i个结点开始的j个结点复制到str { q = (LinkStrNode*)malloc(sizeof(LinkStrNode)); q->data = p->data; r->next = q; r = q; p = p->next; } r->next = NULL; //尾结点的next域置为空 return str; }
LinkStrNode* InsStr(LinkStrNode* s, int i, LinkStrNode* t) { int k; LinkStrNode* str, * p = s->next, * p1 = t->next, * q, * r; str = (LinkStrNode*)malloc(sizeof(LinkStrNode)); str->next = NULL; //置结果串str为空串 r = str; //r指向结果串的尾结点 if (i <= 0 || i > StrLength(s)) return str; //参数不正确时返回空串 for (k = 1; k < i; k++) //将s的前i个结点复制到str { q = (LinkStrNode*)malloc(sizeof(LinkStrNode)); q->data = p->data; r->next = q; r = q; p = p->next; } while (p1 != NULL) //将t的所有结点复制到str { q = (LinkStrNode*)malloc(sizeof(LinkStrNode)); q->data = p1->data; r->next = q; r = q; p1 = p1->next; } while (p != NULL) //将p结点及其后的结点复制到str { q = (LinkStrNode*)malloc(sizeof(LinkStrNode)); q->data = p->data; r->next = q; r = q; p = p->next; } r->next = NULL; //尾结点的next域置为空 return str; }
LinkStrNode* DelStr(LinkStrNode* s, int i, int j) { int k; LinkStrNode* str, * p = s->next, * q, * r; str = (LinkStrNode*)malloc(sizeof(LinkStrNode)); str->next = NULL; //置结果串str为空串 r = str; //r指向结果串的尾结点 if (i <= 0 || i > StrLength(s) || j<0 || i + j - 1>StrLength(s)) return str; //参数不正确时返回空串 for (k = 1; k < i; k++) //将s的前i-1个结点复制到str { q = (LinkStrNode*)malloc(sizeof(LinkStrNode)); q->data = p->data; r->next = q; r = q; p = p->next; } for (k = 0; k < j; k++) p = p->next; //让p沿next跳j个结点 while (p != NULL) //将p结点及其后的结点复制到str { q = (LinkStrNode*)malloc(sizeof(LinkStrNode)); q->data = p->data; r->next = q; r = q; p = p->next; } r->next = NULL; //尾结点的next域置为空 return str; }
LinkStrNode* RepStr(LinkStrNode* s, int i, int j, LinkStrNode* t) { int k; LinkStrNode* str, * p = s->next, * p1 = t->next, * q, * r; str = (LinkStrNode*)malloc(sizeof(LinkStrNode)); str->next = NULL; //置结果串str为空串 r = str; //r指向结果串的尾结点 if (i <= 0 || i > StrLength(s) || j<0 || i + j - 1>StrLength(s)) return str; //参数不正确时返回空串 for (k = 1; k < i; k++) //将s的前i-1个结点复制到str { q = (LinkStrNode*)malloc(sizeof(LinkStrNode)); q->data = p->data; r->next = q; r = q; p = p->next; } for (k = 0; k < j; k++) p = p->next; //让p沿next跳j个结点 while (p1 != NULL) //将t的所有结点复制到str { q = (LinkStrNode*)malloc(sizeof(LinkStrNode)); q->data = p1->data; r->next = q; r = q; p1 = p1->next; } while (p != NULL) //将p结点及其后的结点复制到str { q = (LinkStrNode*)malloc(sizeof(LinkStrNode)); q->data = p->data; r->next = q; r = q; p = p->next; } r->next = NULL; //尾结点的next域置为空 return str; }
void DispStr(LinkStrNode* s)
{
LinkStrNode* p = s->next;
while (p != NULL)
{
printf("%c", p->data);
p = p->next;
}
printf("\n");
}
int main() { LinkStrNode* s, * t, * str; char cstr1[] = "Can you speak Chinese?"; //生成串s和t printf("-----生成串s和t-----\n"); StrAssign(s, cstr1); printf("s:"); DispStr(s); printf("s.length=%d\n", StrLength(s)); char cstr2[] = "Yes,I can!"; StrAssign(t, cstr2); printf("t:"); DispStr(t); printf("t.length=%d\n", StrLength(t)); //串的连接 printf("\n-----串的连接-----\n"); str = Concat(s, t); printf("str:"); DispStr(str); printf("str.length=%d\n", StrLength(str)); //串的复制 printf("\n-----串的复制-----\n"); StrCopy(s, t); printf("s:"); DispStr(s); printf("s.length=%d\n", StrLength(s)); //判断串相等 printf("\n-----判断串相等-----\n"); printf("串s和t相等吗?— %d\n", StrEqual(s, t)); //求子串 printf("\n-----求子串-----\n"); str = SubStr(s, 6, 4); printf("str:"); DispStr(str); printf("str.length=%d\n", StrLength(str)); //子串的删除 printf("\n-----子串的删除-----\n"); str = DelStr(s, 4, 6); printf("str:"); DispStr(str); printf("str.length=%d\n", StrLength(str)); //子串的插入 printf("\n-----子串的插入-----\n"); str = RepStr(s, 5, 6, t); printf("str:"); DispStr(str); printf("str.length=%d\n", StrLength(str)); return 0; }
- 以上算法中的插入均采用尾插法创建。
- 运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。