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)初期: 一. ...
随机推荐
- 1726: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 835 Solved: 398[S ...
- 你真的会 snapshot 吗? - 每天5分钟玩转 OpenStack(163)
这是 OpenStack 实施经验分享系列的第 13 篇. instance snapshot 操作可用于备份或者将 instance 保存为新的 image.如果在生产系统中执行 snapshot ...
- hdoj1072 Nightmare bfs
题意:在一个地图里逃亡,2是起点,3是终点,1是路,0是墙,逃亡者携带一个炸弹,6分钟就会炸,要在6分钟前到达4可以重制时间,问是否能逃亡,若能则输出最小值 我的思路:bfs在5步内是否存在3,存在则 ...
- Unity中溶解shader的总结
在实际的游戏工程中,经常美术和策划会提出溶解的表现要求.比如子弹在飞行的时候,弹道不断的消融:角色受到大型炮弹的攻击,在击飞的时候不断的消融等等诸如此类的表现.一般的消融都是结合粒子系统来实现,通过给 ...
- http的几种请求的方式(Get、Post、Put、Head、Delete、Options、Trace和Connect)
http的这几种请求方式各有各的特点,适用于各自的环境.下面我就说说这些方式的各自特点: 1.Get:它的原理就是通过发送一个请求来取得服务器上的某一资源.获取到的资源是通过一组HTTP头和呈现数据来 ...
- RxJava Map操作详解
2016-06-06 RxJava是最近两年火起来的一个框架,核心是异步,但是对于我来说印象最深的是响应式编程的思想.最近刚好想把自己的项目改成用RxJava实现,所以就研究了下.抛物线和大头鬼两位大 ...
- JavaScript复习之--javascript数据类型隐式转换
JavaScript数据类型隐式转换.一,函数类 isNaN() 该函数会对参数进行隐式的Number()转换,如果转换不成功则返回true. alert() 输出的内容隐式的 ...
- (转)经验分享:CSS浮动(float,clear)通俗讲解
很早以前就接触过CSS,但对于浮动始终非常迷惑,可能是自身理解能力差,也可能是没能遇到一篇通俗的教程. 前些天小菜终于搞懂了浮动的基本原理,迫不及待的分享给大家. 写在前面的话: 由于CSS内容比较多 ...
- 百度cdn资源公共库共享及常用开发接口
CDN公共库是指将常用的JS库存放在CDN节点,以方便广大开发者直接调用 网站:http://cdn.code.baidu.com/ 常用资源: jquery: http://libs.baidu.c ...
- juddi学习一
一.下载juddi 地址:https://mirrors.tuna.tsinghua.edu.cn/apache/juddi/juddi/3.3.4/ 二. 解压下载文件打开目录下的 进入bin目录, ...