串的抽象数据类型
ADT 串(string)
Data
串中元素仅由一个字符组成,相邻元素具有前去和后继的关系
Operation
StrAssign(T,*char):形成一个其值等于字符串常量chars的串T
StrCopy(T,S):串S存在,由串S复制得串T
ClearString(S):串S存在,将串清空
StringEmpty(S):若串S存在,返回true,否则返回false
StringLength(S):返回串S的元素个数,即串的长度
Strcompare(S,T):若S>T,返回值大于0;若S = T,返回0,若S < T ,返回值小于0
Concat(T,S1,S2):用T返回由S1和S2联结而成的新串
SubString(Sub,S,pos,len):串S存在,1<=pos<=StringLength(S),且1<=len<=StringLength(S) + pos -1,
用Sub返回串S的第po个字符起长度为len的子串
Index(S,T,pos):串S和T存在,T是非空串,1<=pos<=StringLength(S),若主串S中存在和串T值相同的子串,
则返回第pos个字符之后第一次出现的位置,否则返回0
StrInsert(S,pos,T):串S和T存在,1<=pos<=StringLength(S)+1.在串S的第pos个字符之前插入串T
StrDelete(S,pos,len):串S存在,1<=pos<=StringLength(S) - len + 1。从串S中删除第pos个字符起长度为len的子串
endADT
test.hpp
#ifndef TEST_H_
#define TEST_H_
#include<cstring>
#include"malloc.h"
#define MAXSIZE 100
typedef char ElemType;
typedef struct
{
ElemType data[MAXSIZE + 1];
int length;
}String;
//StrAssign(T, *char) :形成一个其值等于字符串常量chars的串T
String * strAssign(char * s);
//StrCopy(T, S) : 串S存在,由串S复制得串T
void strCopy(String * S,const String T);
//ClearString(S) : 串S存在,将串清空
void clearString(String * S);
//StringEmpty(S) : 若串S存在,返回true,否则返回false
bool stringEmpty(const String S);
//StringLength(S) : 返回串S的元素个数,即串的长度
int stringLength(const String S);
//Strcompare(S, T) : 若S>T,返回值大于0;若S = T,返回0,若S < T ,返回值小于0
int strCompare(const String S, const String T);
//Concat(T, S1, S2) :用T返回由S1和S2联结而成的新串
String * conCat(const String S1, const String S2);
//SubString(Sub, S, pos, len) : 串S存在,1 <= pos <= StringLength(S), 且1 <= len <= StringLength(S) + pos - 1,
//用Sub返回串S的第pos个字符起长度为len的子串
String * subString(const String S,int pos, const int len);
//KMP之next数组
void getNext(const String S, int *next);
//KMP_Index()
int KMP_index(const String S, const String T, const int pos);
//Index(S, T, pos) : 串S和T存在,T是非空串,1 <= pos <= StringLength(S),若主串S中存在和串T值相同的子串,
//则返回第pos个字符之后第一次出现的位置,否则返回0
int index(const String S, const String T, const int pos);
//StrInsert(S, pos, T) : 串S和T存在,1 <= pos <= StringLength(S) + 1.在串S的第pos个字符之前插入串T
String * strInsert(String * S, int pos, const String T);
//StrDelete(S, pos, len) : 串S存在,1 <= pos <= StringLength(S) - len + 1。从串S中删除第pos个字符起长度为len的子串
void strDelete(String * S, const int pos, const int len);
#endif // !TEST_H_
#pragma once
test.cpp
#include"test.h"
//StrAssign(T, *char) :形成一个其值等于字符串常量chars的串T
String * strAssign(char * s) {
int len = strlen(s);
if (len > MAXSIZE)
return nullptr;
String * temp = (String *)malloc(sizeof(String));
temp->length = 0;
for (int i = 0; i < len; i++)
{
temp->data[i] = s[i];
++temp->length;
}
return temp;
}
//StrCopy(T, S) : 串S存在,由串S复制得串T
void strCopy(String * S, const String T) {
if (S == nullptr)
return;
S->length = 0;
for (int i = 0; i < T.length; i++)
{
S->data[i] = T.data[i];
++S->length;
}
}
//ClearString(S) : 串S存在,将串清空
void clearString(String * S) {
if (S != nullptr)
free(S);
}
//StringEmpty(S) : 若串S存在,返回true,否则返回false
bool stringEmpty(const String S) {
if (S.length != 0)
return false;
else
return true;
}
//StringLength(S) : 返回串S的元素个数,即串的长度
int stringLength(const String S) {
return S.length;
}
//Strcompare(S, T) : 若S>T,返回值大于0;若S = T,返回0,若S < T ,返回值小于0
int strCompare(const String S, const String T) {
if (S.length == T.length)
{
for (int i = 0; i < S.length; i++)
{
if (S.data[i] > T.data[i])
return 1;
else if (S.data[i] < T.data[i])
return -1;
}
return 0;
}
else if (S.length < T.length)
{
for (int i = 0; i < S.length; i++)
{
if (S.data[i] > T.data[i])
return 1;
}
return -1;
}
else
{
for (int i = 0; i < S.length; i++)
{
if (S.data[i] < T.data[i])
return -1;
}
return 1;
}
}
//Concat(T, S1, S2) :用T返回由S1和S2联结而成的新串
String * conCat(const String S1, const String S2) {
if (S1.length + S2.length > MAXSIZE)
return nullptr;
String * S = (String *)malloc(sizeof(String));
S->length = 0;
int i;
for (i = 0; i < S1.length; i++)
{
S->data[i] = S1.data[i];
++S->length;
}
for (int j = 0; j < S2.length; j++)
{
S->data[i] = S2.data[j];
++S->length;
++i;
}
return S;
}
//SubString(Sub, S, pos, len) : 串S存在,1 <= pos <= StringLength(S), 且1 <= len <= StringLength(S) + pos - 1,
//用Sub返回串S的第pos个字符起长度为len的子串
String * subString(const String S,int pos, const int len) {
if (S.length < 1)
return nullptr;
String * Sub = (String *)malloc(sizeof(String));
Sub->length = 0;
if (0 <= pos <= stringLength(S) && 0 <= len <= stringLength(S) + pos - 1)
{
for (int i = 0; i < len; i++)
{
Sub->data[i] = S.data[pos-1];
++Sub->length;
++pos;
}
}
return Sub;
}
//KMP之next数组
void getNext(const String S, int *next) {
int i = 1, j = 0;
int len = stringLength(S);
next[0] = 1;
while (i < len)
{
if (j == 0 || S.data[i - 1] == S.data[j - 1])
{
++i;
++j;
next[j - 1] = j;
}
else
j = next[j - 1];
}
}
//KMP_Index()
int KMP_index(const String S, const String T, const int pos) {
int i = pos;
int j = 0;
int next[255];
getNext(T, next);
int lenS = stringLength(S);
int lenT = stringLength(T);
while (i < lenS && j < lenT)
{
if (S.data[i] == T.data[j])
{
++i;
++j;
}
else
j = next[j];
}
if (j > lenT)
return i - lenT;
else
return 0;
}
//Index(S, T, pos) : 串S和T存在,T是非空串,1 <= pos <= StringLength(S),若主串S中存在和串T值相同的子串,
//则返回第pos个字符之后第一次出现的位置,否则返回0
int index(const String S, const String T, const int pos) {
if (S.length < 1 || T.length < 1)
return -1;
if(0 <= pos <= stringLength(S))
{
//S当前位置下标
int i = pos;
//T当前位置下标
int j = 0;
while (stringLength(S) >= i && stringLength(T) >= j)
{
if (S.data[i] == T.data[j])
{
++i;
++j;
}
else
{
i = i - j + 1;
j = 0;
}
}
if (j > T.length)
return i - T.length;
}
return 0;
}
//StrInsert(S, pos, T) : 串S和T存在,1 <= pos <= StringLength(S) + 1.在串S的第pos个字符之前插入串T
String * strInsert(String * S, int pos, const String T) {
if (S == nullptr)
return nullptr;
if (T.length < 1)
return nullptr;
int len = stringLength(*S);
String * sub = subString(*S, pos, len - pos + 1);
int j = 0;
S->length = pos -1;
for (int i = pos - 1; i < pos + stringLength(T) - 1; i++)
{
S->data[i] = T.data[j];
++S->length;
++j;
}
String * newString = conCat(*S, *sub);
return newString;
}
//StrDelete(S, pos, len) : 串S存在,1 <= pos <= StringLength(S) - len + 1。从串S中删除第pos个字符起长度为len的子串
void strDelete(String * S,const int pos, const int len) {
if (S == nullptr)
return;
int lenS = stringLength(*S);
int i = pos;
S->length = pos;
while (i + len < lenS)
{
S->data[i] = S->data[i + len];
++S->length;
++i;
}
}
main.cpp
#include<iostream>
#include"test.h"
void showString(const String S);
int main()
{
char s[10] = { 'a','b','c','d','e','f' };
String * S = strAssign(s);
std::cout << "字符串是否为空: " << stringEmpty(*S) << std::endl;
std::cout << "字符串的长度为: " << stringLength(*S) << std::endl;
showString(*S);
String * T = (String *)malloc(sizeof(String));
strCopy(T, *S);
std::cout << "字符串是否为空: " << stringEmpty(*T) << std::endl;
std::cout << "字符串的长度为: " << stringLength(*T) << std::endl;
showString(*T);
char s1[10] = { 'a','b','c','d','f'};
String * S1 = strAssign(s1);
std::cout<<"S是否比S1大: "<<strCompare(*S, *S1)<<std::endl;
String * L = conCat(*S, *S1);
showString(*L);
String * TL = subString(*L, 2, 6);
showString(*TL);
std::cout << index(*L, *S1, 1)<<std::endl;
String * newString = strInsert(S,3,*S1);
showString(*newString);
strDelete(newString, 2, 3);
showString(*newString);
return 0;
}
void showString(const String S) {
if (S.length == 0)
{
std::cerr << "字符串为空\n";
return;
}
for (int i = 0; i < S.length; i++)
{
std::cout << S.data[i] << " ";
}
std::cout << std::endl;
}
运行结果: