串(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. asp.net mvc源码分析-Route的GetRouteData

    我知道Route这里东西应该算路由,这里把它放到mvc里面有些不怎么合适,但是我想大家多数遇到路由都是在mvc的时候吧.首先我们还是来看看GetRouteData方法吧 [csharp] public ...

  2. KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架之koahub-handlebars

    koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...

  3. 在node中使用 ES6

    ES6+ 太棒了,但是很多高级功能node是不支持的,就需要使用babel转换成ES5, 1.安装babel依赖 npm install babel-core --save-dev 2.安装babel ...

  4. 算法模板——Tarjan强连通分量

    功能:输入一个N个点,M条单向边的有向图,求出此图全部的强连通分量 原理:tarjan算法(百度百科传送门),大致思想是时间戳与最近可追溯点 这个玩意不仅仅是求强连通分量那么简单,而且对于一个有环的有 ...

  5. Java向上转型的意义

    比如我这个程序 public class Testjava{ public static void main(String args[]) { fun(new Student()); fun(new ...

  6. JAVA基础知识(2)--堆栈和递归的操作

    2015-07-26 18:16:21/***该应用程序对堆栈和递归方法进行实例操作: *1.堆栈操作:先进后出,*2.递归方法:直接或者调用自己的方法:*@author lhm *Email:912 ...

  7. 内网转发ngrok的使用

    1.下载解压ngrok:https://ngrok.com/download 2.执行ngrok会打开控制台 3.输入命令,开始映射本地的8080端口 ngork http 8080 控制台会返回一个 ...

  8. centos6.5安装配置supervisor

    1.下载并安装supervisor https://pypi.python.org/pypi/supervisor/3.2.0 .tar.gz cd supervisor- python setup. ...

  9. vue学习笔记 样式 class style(五)

    使用v-bind数据绑定class和style,v-bind:class可以与传统的class属性共存,其中可以用{}设置多个class,根据条件判断的语法是class名:条件,带-的class名需要 ...

  10. 简单实用的JQuery弹出层

    效果: 初始状态时滚动条是可以滚动的 弹出层出现之后:1.弹窗始终居于整个窗口的中间 2.滚动条不可滚动 实现代码: HTML代码: <div class="container&quo ...