串(string)是n(n>=0)个字符组成的有限序列。

  由于串中的字符都是连续存储的,在C#中有恒定不变的特性。一经创建就保持不变。

  为了区别C#中的string,因此以stringDS类模拟string的数据结构,代码如下:

class stringDS
{
private char[] _data;
//字符数组 //索引器
public char this[int index] {
get {
return _data[index];
}
set {
_data[index] = value;
}
} //构造函数
public stringDS(char[] arr)
{
_data = new char[arr.Length];
for (int i = ; i < arr.Length; i++) {
_data[i] = arr[i];
}
} //构造函数
public stringDS(int len)
{
char[] arr = new char[len];
_data = arr;
} //求串长
public int GetLength()
{
return _data.Length;
} //串比较
public int Compare(stringDS s)
{
int len = (this.GetLength() <= s.GetLength()) ? this.GetLength() : s.GetLength();
// len=len-1;
int i = ;
for(i=;i<len;i++)
{
if(this[i]!=s[i])
{
break;
}
}
if (i < len) {
if (this[i] < s[i]) {
return -;
} else if (this[i] > s[i]) {
return ;
}
} else if (this.GetLength() == s.GetLength()) {
return ;
} else if (this.GetLength() < s.GetLength()) {
return -;
}
return ;
} //求子串
public stringDS SubString(int index, int len)
{
if (index < || index > (this.GetLength() - )
|| len < || len > (this.GetLength() - index)) {
Console.WriteLine("position or len is error!");
return null;
}
stringDS s = new stringDS(len);
for (int i = ; i < len;++i) {
s[i] = this[i + index];
}
return s;
} //串连接
public stringDS Concat(stringDS s)
{
stringDS s1 = new stringDS(this.GetLength() + s.GetLength());
for (int i = ; i < this.GetLength(); i++) {
s1._data[i] = this[i];
}
for (int i = ; i < s.GetLength(); i++) {
s1._data[this.GetLength() + i] = s[i];
}
return s1;
} //串插入
public stringDS Insert(int index, stringDS s)
{
int len = s.GetLength();
int len2 = len + this.GetLength();
stringDS s1 = new stringDS(len2);
if (index < || index > this.GetLength() - ) {
Console.WriteLine("Position is error!");
return null;
}
for (int i = ; i < index; i++) {
s1[i] = this[i];
}
for (int i = index; i < index + len; i++) {
s1[i] = s[i - index];
}
for (int i = index + len; i < len2; i++) {
s1[i] = this[i - len];
}
return s1;
} //串删除
public stringDS Delete(int index, int len)
{
if (index < || index > (this.GetLength() - )
|| len < || len > (this.GetLength() - index)) {
Console.WriteLine("position or len is error!");
return null;
}
stringDS s = new stringDS(this.GetLength() - len);
for (int i = ; i < index; i++) {
s[i] = this[i];
}
for (int i = index + len; i < this.GetLength(); i++) {
s[i] = this[i];
}
return s;
} //串定位
public int Index(stringDS s)
{
if (this.GetLength() < s.GetLength()) {
Console.WriteLine("There is not string s!");
return -;
}
int i = ;
int len = this.GetLength() - s.GetLength();
while (i< len) {
stringDS temp=this.SubString(i,s.GetLength());
if (temp.Compare(s) == ) {
break;
}
i++;
}
if (i <= len) {
return i;
}
return -;
} }

C#数据结构之串的更多相关文章

  1. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. 【Java】 大话数据结构(8) 串的模式匹配算法(朴素、KMP、改进算法)

    本文根据<大话数据结构>一书,实现了Java版的串的朴素模式匹配算法.KMP模式匹配算法.KMP模式匹配算法的改进算法. 1.朴素的模式匹配算法 为主串和子串分别定义指针i,j. (1)当 ...

  3. javascript实现数据结构:串--定长顺序存储表示以及kmp算法实现

    串(string)(或字符串)是由零个或多个字符组成的有限序列.串中字符的数目称为串的长度.零个字符的串称为空串(null string),它的长度为零. 串中任意个连续的字符组成的子序列称为该串的子 ...

  4. javascript实现数据结构:串--堆分配存储表示

    堆分配存储表示 这种存储表示的特点是,仍以一组地址连续的存储单元存放串值字符序列,但它们的存储空间是在程序执行过程中动态分配而得. 结构图: 实现: function HString(){ this. ...

  5. 数据结构-模式匹配串算法(KMP)

    #include<cstdio> #include<iostream> #include<string> #include<cstring> #incl ...

  6. 大话数据结构(8) 串的模式匹配算法(朴素、KMP、改进算法)

    --喜欢记得关注我哟[shoshana]-- 目录 1.朴素的模式匹配算法2.KMP模式匹配算法 2.1 KMP模式匹配算法的主体思路 2.2 next[]的定义与求解 2.3 KMP完整代码 2.4 ...

  7. SDUST数据结构 - chap4 串

    函数题: 6-1 查找子串: 裁判测试程序样例: #include <stdio.h> #define MAXS 30 char *search(char *s, char *t); vo ...

  8. poj分类 很好很有层次感。

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  9. 【转】POJ题目分类推荐 (很好很有层次感)

    OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...

随机推荐

  1. 2.css字体单位

    这期简单说说css字体单位 字体单位有三种:px.em.rem 任意浏览器的默认字体高都是16px.所有未经调整的浏览器都符合: 1em=16px. % 百分比 in 英寸 cm 厘米 mm 毫米 e ...

  2. Ajax页面的加载数据与删除

    1.数据库找一张表: 颜色表2.主页面主页面的代码用到tbody:TBODY作用是:可以控制表格分行下载,从而提高下载速度.(网页的打开是先表格的的内容全部下载完毕后,才显示出来,分行下载可以先显示部 ...

  3. 1819: [JSOI]Word Query电子字典

    1819: [JSOI]Word Query电子字典 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 729  Solved: 238[Submit][S ...

  4. iOS回顾笔记(06) -- AutoLayout从入门到精通

    iOS回顾笔记(06) -- AutoLayout从入门到精通 随着iOS设备屏幕尺寸的增多,当下无论是纯代码开发还是Xib/StoryBoard开发,自动布局已经是必备的开发技能了. 我使用自动布局 ...

  5. linux 部署jenkins

    1.安装jdk,配置jdk路径,python路径 (当前用户的配置文件)  vi .bash_profile export JAVA_HOME=$HOME/local/jdk1.8.0_111 exp ...

  6. Android OS体系结构详解

    Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成,号称是首个为移动终端打造的真正开放和完整的移动软件. 架构详解 下 ...

  7. 【G】系列导航

    G.开源的分布式部署解决方案 [G]开源的分布式部署解决方案 - 预告篇 [G]开源的分布式部署解决方案(一) - 开篇 [G]开源的分布式部署解决方案(二) - 好项目是从烂项目基础上重构出来的 [ ...

  8. 适用MySQL Migration Toolkit 1.0 将oracle迁移到mysql中遇到的问题

    这里主要说一下我在适用中碰到的问题,主要过程参考 http://www.cnblogs.com/duwenlei/p/3520759.html. 首先启动MySQLMigrationTool.exe ...

  9. ECMA script 6的新特性

    简单介绍下ES6的新特性: (1)箭头操作符 :简化了函数的书写 (2)类的支持:引入了class关键字,对象的创建,继承更加直观,父类方法的调用,实例化,构造函数等概念更加形象化. (3)增强的对象 ...

  10. 5种方法推导Normal Equation

    引言: Normal Equation 是最基础的最小二乘方法.在Andrew Ng的课程中给出了矩阵推到形式,本文将重点提供几种推导方式以便于全方位帮助Machine Learning用户学习. N ...