您的位置:首页 > 理论基础 > 数据结构算法

javascript数据结构系列(五)-串

2017-08-24 23:02 316 查看

前言

昨天和今天本来打算仔细研究一下严蔚敏教材的离散事件模拟,结果经过两天发现还是没法儿理解,先行放下日后再说,今天先学习串。

1. 定义:串是由0个或多个字符组成的有限序列,一般记为:s=′a1a2a3a4⋅⋅⋅a′n(n≥0)其中,s是串的名,用单引号括起来的字符序列是串的值;a1(1≤i≤n)可以是字母,数字或者其他字符;串中字符的数目n称为串的长度,0个字符的串称为空串,它的长度为0。串中任意个连续的字符组成的子序列称为该串的子串。包含子串的串响应的称为主串。通常称字符在序列中的序号为该字符在串中的位置。子串在主串中的位置则以子串的第一个字符在主串中的位置来表示。称两个串是相等的,当且仅当这两个串的值相等。也就是说,只有当两个串的长度相等,并且各个对应位置的字符都相等时才相等。

2. 实现

//串的块存储
function Chunk(chunkSize){
this.chunkSize = chunkSize || 4;
this.chunk = [];
for(var i = 0; i<this.chunkSize;i++){
this.chunk[i] = '#'
}
//type:chunk
this.next = null;
};
exports.LString = LString;
function LString(chunkSize){
this.head = null;
this.tail = null;
this.length = 0;
this.chunkSize = chunkSize || 4;
};
LString.prototype = {
//生成一个串
strAssign:function(chars){
this.head = this.tail = new Chunk(this.chunkSize);
this.length = chars.length;
var current = this.head;
for(var i = 0,len = chars.length;i<len;i++){
current.chunk[i % this.chunkSize] = chars[i];
if((i+1 < len) && ((i+1) % this.chunkSize) === 0){
current.next = new Chunk();
current = current.next;
}
}
this.tail = current;
},
//复制一个串
strCopy:function(S){

},
//判断是否为空
strEmpty:function(){

},
//若S>T则返回值大于0
strCompare:function(){
var current = this.head;
var curT = tLString.head;
if(this.lenth !== tLString.length){
return false;
}
while(current){
for(var i = 0;i < this.chunkSize;i++){
if(current.chunk[i] !== curT.chunk[i]){
return false;
}
}
current = current.next ;
curT = curT.next;
}
return true;
},
//求串的长度
strLength:function(){

},
//返回pos开始长度为len的子串
subString:function(){

},
//链接两个字符串
contact:function(tLString){

},
//若s中存在与t相同的子串,则返回他在主串中第ps个字符之后第一次出现的位置
index:function(){

},
//在s的第pos个个字符之前插入t
strInsert:function(){

},
//从s中删除第pos个字符起长度为len的子串
strDelete:function(){

},
};


3. 注:没有实现的方法明天再继续。今天这两个还有点懵。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: