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)初期: 一. ...
随机推荐
- BFC原理
一.BFC是什么? 在解释 BFC 是什么之前,需要先介绍 Box.Formatting Context的概念. Box: CSS布局的基本单位 Box 是 CSS 布局的对象和基本单位, 直观点来说 ...
- Junit使用教程
Junit是Java的单元测试工具,同时也是极限编程的好帮手.Junit4借助于Java5的Annotation(标注类)和静态导入的新特性,与Junit3有很大的区别,所以建议初学者直接使用Juni ...
- MAVEN学习(初级)
1. 项目管理利器 MAVEN 学习,参考慕课网 :http://www.imooc.com/search/?words=maven 2. 下载MEAVN 地址:http://maven.apache ...
- 基于Spring-WS的Restful API的集成测试
在很多Java企业级应用中,Spring占据了非常重要的位置,这就导致了基本上的技术选型都是围绕着Spring来, 比方说笔者最近的项目需要开发一个Restful的API接口,选型的时候就说,客户架构 ...
- 【HAL库每天一例】freemodbus移植
例程下载:资料包括程序.相关说明资料以及软件使用截图 百度云盘:https://pan.baidu.com/s/1slN8rIt 密码:u6m1 360云盘:https://yunpan.cn/OcP ...
- C++ Primer 5 CH1 开始
1.1 编写一个简单的C++程序 在大多数系统中,main 的返回值被用来指示状态.返回值 0 表示成功,非 0 的返回值的含义由系统定义,通常用来指出错误类型. 访问 main 的返回值的方法依赖于 ...
- mac下安装 resin 奇葩问题总结
mac 下安装 resin(需要确认电脑配置好了 java 环境变量): 第一步:去 resin 的官网下载最新的压缩包,官网连接:http://caucho.com/products/resin/d ...
- [原创]ASM动态修改JAVA函数之函数字节码初探
ASM是非常强大的JAVA字节码生成和修改工具,具有性能优异.文档齐全.比较易用等优点.官方网站:http://asm.ow2.org/ 要想熟练的使用ASM,需要对java字节码有一定的了解,本文重 ...
- 老司机实战Windows Server Docker:3 单节点Windows Docker服务器简单运维(上)
经过上两篇实战Windows Server Docker系列文章,大家对安装Windows Docker服务以及如何打包现有IIS应用为docker镜像已经有了基本认识.接下来我们来简单讲讲一些最基本 ...
- Hive 伪分布式下安装
本安装过程只作为个人笔记用,非标准教程,请酌情COPY.:-D Hive下载 下载之前,需先查看兼容的Hadoop版本,并安装hadoop,参考 http://www.cnblogs.com/yong ...