[数据结构-严蔚敏版]P71串的抽象数据类型的定义
Posted Wecccccccc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[数据结构-严蔚敏版]P71串的抽象数据类型的定义相关的知识,希望对你有一定的参考价值。
代码如下:
#include <iostream>
#include <string>
using namespace std;
typedef struct
{
char *ch;
int length;
}String;
bool initString(String &s)
{
s.ch = nullptr;
s.length = 0;
return true;
}
bool strAssign(String &s, const char *ch)
{
int len = strlen(ch);
s.ch = new char[len + 1];
if (!s.ch) return false;
for (int i = 0; i < len; i++)
{
s.ch[i] = ch[i];
}
s.ch[len] = '\\0';
s.length = len;
return true;
}
bool strCopy(String &s, String t)
{
if (!t.ch) return false;
if (s.ch) delete[] s.ch;
int len = strlen(t.ch);
s.ch = new char[len + 1];
for (int i = 0; i < len; i++)
{
s.ch[i] = t.ch[i];
}
s.ch[len] = '\\0';
s.length = len;
return true;
}
bool strEmpty(String s)
{
if (s.ch && s.length == 0) return true;
return false;
}
bool strCompare(String s, String ss)
{
if (!s.ch) return false;
if (!ss.ch) return false;
for (int i = 0; i < s.length && i < ss.length; i++)
{
if (s.ch[i] != ss.ch[i])
{
return s.ch[i] - ss.ch[i];
}
}
return s.length - ss.length;
}
int strLength(String s)
{
return s.length;
}
bool clearString(String &s)
{
s.length = 0;
return true;
}
bool concatString(String &s, String s1, String s2)
{
if (s.ch) delete[] s.ch;
int len1 = s1.length;
int len2 = s2.length;
int len3 = len1 + len2;
s.ch = new char[len3 + 1];
for (int i = 0; i < len1; i++)
{
s.ch[i] = s1.ch[i];
}
for (int i = len1; i < len3; i++)
{
s.ch[i] = s2.ch[i];
}
s.ch[len3] = '\\0';
s.length = len3;
return true;
}
bool subString(String &sub, String s, int pos, int len)
{
if (!s.ch) return false;
if (pos <1 || pos > s.length || len < 0 || len > s.length - pos + 1) return false;
if (sub.ch) delete[] sub.ch;
if (!len)
{
sub.ch = nullptr;
sub.length = 0;
}
else
{
sub.ch = new char[len + 1];
for (int i = 0; i < len; i++)
{
sub.ch[i] = s.ch[pos - 1 + i];
}
sub.ch[len] = '\\0';
sub.length = len;
}
return true;
}
int indexString(String s, String ss, int pos)
{
if (pos > 0)
{
int n = s.length;
int m = ss.length;
int i = pos;
String sub;
initString(sub);
while (i <= n - m + 1)
{
subString(sub, s, i, m);
if (strCompare(sub, ss) != 0) ++i;
else return i;
}
}
return false;
}
bool strInsert(String &s, int pos, String t)
{
if (t.ch && s.ch)
{
int len1 = t.length;
int len2 = s.length;
int len3 = len1 + len2;
char *p = new char[len3 + 1];
if (!p) return false;
for (int i = 0; i < pos - 1 && i < len3; i++)
{
p[i] = s.ch[i];
}
for (int i = 0; i < len1 && pos - 1 + i; i++)
{
p[pos - 1 + i] = t.ch[i];
}
for (int i = 0; i < len3 - pos + 1 && pos + len1 - 1 + i < len3; i++)//pos + len1 - 1 + i < len3,防止数组越界,导致无法成功释放内存(数组越界以后,delete无法成功释放内存)
{
p[pos + len1 - 1 + i] = s.ch[pos - 1 + i];
}
p[len3] = '\\0';
delete[] s.ch;
s.ch = nullptr;
s.ch = p;
s.length = len3;
return true;
}
return false;
}
bool strDelete(String &s, int pos, int len)
{
if (pos >= s.length) return false;
if (pos + len >= s.length)
{
s.length = len;
s.ch[len] = '\\0';
}
else
{
int start = pos + len - 1;
while (start <= s.length)
{
s.ch[pos - 1] = s.ch[start];
++start;
++pos;
}
s.length -= len;
}
return true;
}
bool replaceString(String &s, String t, String v)
{
int len_s = s.length;
int len_t = t.length;
int len_v = v.length;
int j = 1;
String sub;
initString(sub);
while (len_s - j + 1 >= len_t)
{
subString(sub, s, j, len_t);
if (strCompare(sub, t) == 0)
{
strDelete(s, j, len_t);
strInsert(s, j, v);
j += len_v;
s.length = s.length + len_v - len_t;
len_s = s.length;
}
else
{
j++;
}
}
return true;
}
bool destroyString(String &s)
{
if (s.ch)
{
delete[]s.ch;
s.length = 0;
}
return true;
}
int main()
{
const char *ch = "dasfas";
String s1;
const char *ch1 = "dadas";
String s2;
const char *ch2 = "as";
String s3;
strAssign(s3, ch2);
strAssign(s2, ch1);
strAssign(s1, ch);
strInsert(s1, 4, s2);
strDelete(s1, 4, 5);
cout << s1.ch << endl;
cout << s2.ch << endl;
replaceString(s1, s3, s2);
cout << s1.ch << endl;
String s4;
initString(s4);
strCopy(s4, s1);
cout << s4.ch << endl;
return 0;
}
以上是关于[数据结构-严蔚敏版]P71串的抽象数据类型的定义的主要内容,如果未能解决你的问题,请参考以下文章