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. 注:没有实现的方法明天再继续。今天这两个还有点懵。
相关文章推荐
- 【脚本语言系列】关于JavaScript数据结构,你需要知道的事
- Javascript与数据结构系列(一)——栈的实现
- javascript数据结构系列(一)-线性表
- javascript数据结构系列(六)-树和二叉树(1)
- Javascript与数据结构系列(二)——队列的实现
- javascript数据结构系列(五)-串(3)
- javascript数据结构系列(六)-二叉树(2)
- 从零开始学_JavaScript_系列(45)——ES6新增数据结构:Map和WeakMap
- JavaScript中的数据结构及实战系列(1):队列
- JavaScript中的数据结构及实战系列
- 从零开始学_JavaScript_系列(44)——ES6新增数据结构:Set类型和WeakSet
- javascript数据结构系列(三)-栈
- JavaScript中的数据结构及实战系列(1):队列
- JavaScript中的数据结构及实战系列(2):栈
- javascript数据结构系列(二)-线性表(1)
- 深入理解javascript作用域系列第四篇——块作用域
- 每周优秀代码赏析系列【一】----LINQ In Javascript
- 深入理解webkit内核系列第二篇:JavaScript引擎深度解析
- 深入理解JavaScript系列(43):设计模式之状态模式
- JavaScript系列文章:详解正则表达式基本知识