0%

数据结构-串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#define MaxLength 60
typedef struct /*串的定义*/
{
char str[MaxLength];
int length;
} SeqString;

void StrAssign(SeqString *S, char cstr[])
/*串的赋值操作*/
{
int i = 0;
for (i = 0; cstr[i] != '\0'; i++) /*将常量cstr中的字符赋值给串S*/
S->str[i] = cstr[i];
S->length = i;
}
int StrEmpty(SeqString S)
{
if (S.length == 0) /*判断串的长度是否等于0*/
return 1; /*当串为空时,返回1;否则返回0*/
else
return 0;
}
int StrLength(SeqString S) /*求串的长度操作*/
{
return S.length;
}
void StrCopy(SeqString *T, SeqString S) /*串的复制操作.*/
{
for (int i = 0; i < S.length; i++) /*将串S的字符赋值给串T*/
T->str[i] = S.str[i];
T->length = S.length; /*将串S的长度赋值给串T*/
}
int StrCompare(SeqString S, SeqString T) /*串的比较操作*/
{
for (int i = 0; i < S.length && i < T.length; i++) /*比较两个串中的字符*/
if (S.str[i] != T.str[i]) /*如果出现字符不同,则返回两个字符的差值*/
return (S.str[i] - T.str[i]);
return (S.length - T.length); /*如果比较完毕,返回两个串的长度的差值*/
}
int StrInsert(SeqString *S, int pos, SeqString T)
/*串的插入操作。在S中第pos个位置插入T分为三种情况*/
{
int i;
if (pos < 0 || pos - 1 > S->length) /*插入位置不正确,返回0*/
{
printf("插入位置不正确");
return 0;
}
if (S->length + T.length <= MaxLength) /*第一种情况,插入子串后串长≤MaxLength,即子串T完整地插入到串S中*/
{
/*在插入子串T前,将S中pos后的字符向后移动len个位置*/
for (i = S->length + T.length - 1; i >= pos + T.length - 1; i--)
S->str[i] = S->str[i - T.length];

for (i = 0; i < T.length; i++) /*将串插入到S中*/
S->str[pos + i - 1] = T.str[i];

S->length = S->length + T.length;
return 1;
}
/*第二种情况,子串可以完全插入到S中,但是S中的字符将会被截掉*/
else if (pos + T.length <= MaxLength)
{
for (i = MaxLength - 1; i > T.length + pos - 1; i--) /*将S中pos以后的字符整体移动到数组的最后*/
S->str[i] = S->str[i - T.length];

for (i = 0; i < T.length; i++) /*将T插入到S中*/
S->str[i + pos - 1] = T.str[i];

S->length = MaxLength;
return 0;
}
/*第三种情况,子串T不能被完全插入到S中,T中将会有字符被舍弃*/
else
{
for (i = 0; i < MaxLength - pos; i++) /*将T直接插入到S中,插入之前不需要移动S中的字符*/
S->str[i + pos - 1] = T.str[i];
S->length = MaxLength;
return 0;
}
}

int StrDelete(SeqString *S, int pos, int len)
/*在串S中删除pos开始的len个字符*/
{
if (pos < 0 || len < 0 || pos + len - 1 > S->length) /*如果参数不合法,则返回0*/
{
printf("删除位置不正确,参数len不合法");
return 0;
}
else
{
for (int i = pos + len; i <= S->length - 1; i++) //将串S的第pos个位置以后的len个字符覆盖
S->str[i - len] = S->str[i];
S->length = S->length - len; /*修改串S的长度*/
return 1;
}
}

int StrCat(SeqString *T, SeqString S) /*将串S连接在串T的后面*/
{
int i, flag;
if (T->length + S.length <= MaxLength)
{
for (i = T->length; i < T->length + S.length; i++)
T->str[i] = S.str[i - T->length];
T->length = T->length + S.length;
flag = 1;
}
else if (T->length < MaxLength)
{
for (i = T->length; i < MaxLength; i++)
T->str[i] = S.str[i - T->length];
T->length = MaxLength;
flag = 0;
}
return flag;
}

int SubString(SeqString *Sub, SeqString S, int pos, int len)
//将串S中的pos个位置起的len个字符取出放入Sub中
{
if (pos < 0 || len < 0 || pos + len - 1 > S.length)
{
printf("参数pos和len不合法");
return 0;
}
else
{
for (int i = 0; i < len; i++)
Sub->str[i] = S.str[i + pos - 1];
Sub->length = len;
return 1;
}
}

int StrIndex(SeqString S, int pos, SeqString T)
/*在主串S中的第pos个位置开始查找子串T,如果找到返回子串在主串的位置;否则,返回-1*/
{
if (StrEmpty(T))
return 0;
int i = pos, j = 0;
while (i < S.length && j < T.length)
{
if (S.str[i] == T.str[j]) /*如果串S和串T中对应位置字符相等,则继续比较下一个字符*/
{
i++;
j++;
}
else /*如果当前对应位置的字符不相等,则从串S的下一个字符开始,T的第0个字符开始比较*/
{
i = i - j + 1;
j = 0;
}
}
if (j >= T.length) /*如果在S中找到串T,则返回子串T在主串S的位置*/
return i - j + 1;
else
return -1;
}
int StrReplace(SeqString *S, SeqString T, SeqString V)
//将串S中的串T用V取代
{
int i = 0, flag;
do
{
i = StrIndex(*S, i, T); //查找子串T
if (i)
{
StrDelete(S, i, StrLength(T));
flag = StrInsert(S, i, V);
if (!flag)
return 0;
i += StrLength(V);
}
} while (i);
return 1;
}
}
/*统计串S2在串S1中出现的次数*/
int Count(SeqString S1, SeqString S2)
{
int n = 1, f = 1, count = 0;
while (f != -1)
{
f = StrIndex(S1, n, S2); /*在串S1中查找串S2的位置*/
n = f;
n++;
if (f != -1) /*如果S2在S1中出现一次,则count加1*/
count++;
}
return count;
}
void StrClear(SeqString *S)
{
S->length = 0;
}
void StrPrint(SeqString S)
{
for (int i = 0; i < S.length; i++)
printf("%c", S.str[i]);
}