数据结构(C语言版) 第4章 串.docVIP

  • 7
  • 0
  • 约9.07千字
  • 约 17页
  • 2019-06-14 发布于江西
  • 举报
PAGE 4 第4章 串 串:限定数据元素类型的线性表。 串与线性表的区别: 线性表 注重单个元素的操作 串 注重整体或部分整体的操作 典型应用实例: 与串结构的关系 编辑软件 字符串的插入、删除、保存 信息检索 病毒查找 字符串匹配 1 逻辑结构 1.1 定义 位串:数据元素属于{0,1} ASCII码串: 数据元素属于ASCII码字符集。 数字串:数据元素属于整数集合 长度 a0a1……an-1的长度为n 空串 长度为0的串: 子串 串中任意个连续字符组成的子序列。 相等 两串长度相等,对应的每个字符相等。 1.2 类定义 class String { public: String( ); String(T a[], int n); // 带参构造函数 String(String s); // 拷贝构造函数:串的赋值 ~String( ); int Length(); // 返回串长度 bool operator(String s); bool operator==(String s); String Substr(int pos,int len); // 返回子串 void Insert(int pos, String s); // 第i个位置上插入s void Delete(int pos,int len); // 删除子串 void Concat(String s); // 连接s int FindBF(String s,int pos); // 返回子串s在串中的位置 int FindKMP(String s,int pos); // 返回子串s在串中的位置 void Replace(String s,String t); // 将子串s替换成t friend ostream operator(ostream o, StringT s); } 2 顺序串 ///////////////////////////////// // 项目路径:7动态顺序串 ///////////////////////////////// 2.1 类定义 class String { T *m_Data; // 存放串元素的连续空间 int m_Size; // 空间大小 int m_Length; // 串长 public: ……… } 2.2 构造/析构(测试函数Test1) template class T StringT:: String( ) { m_Data=NULL; m_Size=m_Length=0; } template class T StringT::String(T a[], int n) { m_Size = m_Length = n; m_Data = new T[m_Size]; if(!m_Data) throw 内存不够,上溢; for(int i=0; im_Length; i++) m_Data[i]=a[i]; } template class T StringT::String(String s) // 拷贝构造函数 { m_Size = m_Length = s.m_Length; m_Data = new T[m_Size]; if(!m_Data) throw 内存不够,上溢; for(int i=0; im_Length; i++) m_Data[i]=s.m_Data[i]; } template class T StringT:: ~String( ) { delete []m_Data; } 2.3 比较(测试函数Test2) // 重载 template class T bool StringT::operator(String s) { for(int i=0; im_Length is.m_Length; i++) { if(m_Data[i]s.m_Data[i]) return true; if(m_Data[i]s.m_Data[i]) return false; } if(m_Lengths.m_Length) return true; return false; } // 重载== template class T bool StringT::operator==(String s) { for(int i=0; im_

文档评论(0)

1亿VIP精品文档

相关文档