C#数据结构之串
串(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#数据结构之串的更多相关文章
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【Java】 大话数据结构(8) 串的模式匹配算法(朴素、KMP、改进算法)
本文根据<大话数据结构>一书,实现了Java版的串的朴素模式匹配算法.KMP模式匹配算法.KMP模式匹配算法的改进算法. 1.朴素的模式匹配算法 为主串和子串分别定义指针i,j. (1)当 ...
- javascript实现数据结构:串--定长顺序存储表示以及kmp算法实现
串(string)(或字符串)是由零个或多个字符组成的有限序列.串中字符的数目称为串的长度.零个字符的串称为空串(null string),它的长度为零. 串中任意个连续的字符组成的子序列称为该串的子 ...
- javascript实现数据结构:串--堆分配存储表示
堆分配存储表示 这种存储表示的特点是,仍以一组地址连续的存储单元存放串值字符序列,但它们的存储空间是在程序执行过程中动态分配而得. 结构图: 实现: function HString(){ this. ...
- 数据结构-模式匹配串算法(KMP)
#include<cstdio> #include<iostream> #include<string> #include<cstring> #incl ...
- 大话数据结构(8) 串的模式匹配算法(朴素、KMP、改进算法)
--喜欢记得关注我哟[shoshana]-- 目录 1.朴素的模式匹配算法2.KMP模式匹配算法 2.1 KMP模式匹配算法的主体思路 2.2 next[]的定义与求解 2.3 KMP完整代码 2.4 ...
- SDUST数据结构 - chap4 串
函数题: 6-1 查找子串: 裁判测试程序样例: #include <stdio.h> #define MAXS 30 char *search(char *s, char *t); vo ...
- poj分类 很好很有层次感。
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
- 【转】POJ题目分类推荐 (很好很有层次感)
OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...
随机推荐
- 基于Struts自定义MVC-1
自定义MVC 数据库:Oracle表:User(id,uname,upwd)自定义Struts框架一.定义Action接口 1 import javax.servlet.http.*; ...
- 关于label和input对齐的那些事
input文本和label对齐 默认状态下,也就是下面这样, 文字和input是居中的. <div> <label>我是中国人</label> <input ...
- JavaScript前端最全API集锦
一.节点1.1 节点属性Node.nodeName //返回节点名称,只读Node.nodeType //返回节点类型的常数值,只读Node.nodeValue //返回Text或Comme ...
- 476. Number Complement
题目 Given a positive integer, output its complement number. The complement strategy is to flip the bi ...
- 一个服务器启动多个tomcat(详细图解)
1.官网下载一个tomcat,复制一个副本(第二个tomcat) 2.添加2个环境变量(右键单击我的电脑->选择属性->选择高级->选择环境变量),是2个tomcat的位置,环境变量 ...
- 双系统删除Ubuntu后出现grub界面而无法正常启动Windows系统的解决方法
第一次安装双系统的时候由于不怎么会弄,设置了ubuntu引导windows,这种方法是非常不推荐的,因为当ubuntu出现问题或者是当你不再使用ubuntu的时候,删除ubuntu就会成为一个很麻烦的 ...
- Linux云自动化运维第二课
一.Linux系统结构 1.Linux是一个倒树结构.Linux中所有的东西都是文件.这些文件都在系统的顶级目录中"/","/"是根目录."/&quo ...
- 最短路径之BF算法+线性规划(图片格式)
- iOS 上传文件
NSString *boundry = @"boundry";//分节符 NSMutableURLRequest *mutableRequest = [NSMutableURLRe ...
- iOS开发之UITableView及cell重用
1.UITanleview有的两种风格 一种是Plain,一种是Grouped,可以从这里设置风格: 他们样式分别如下: Plain: Grouped: 2.tableView展示数据的过程: (1) ...