javascript实现数据结构:串--堆分配存储表示
堆分配存储表示
这种存储表示的特点是,仍以一组地址连续的存储单元存放串值字符序列,但它们的存储空间是在程序执行过程中动态分配而得。
结构图:
实现:
function HString(){
this.ch = {};
this.length = 0;
}
exports.HString = HString;
HString.prototype = {
// 1 <= position <= this.length.在串的第position个字符之前插入串tHString
strInsert: function(position, tHString){
if(position < 1 || position > this.length + 1)
throw new Error('unexpected position'); if(tHString.length){
// 为插入t而腾出位置
for(var i = this.length - 1, len = position - 1; i >= len; --i)
this.ch[i + tHString.length] = this.ch[i];
// s.ch[position - 1..position + tHString.length - 2] = tHString.ch[0..tHString.length - 1];
// for(i = 0, len = tHString.length - 1; i <= len; i++)
// this.ch[position - 1 + i] = tHString.ch[i];
stringCopy(this.ch, tHString.ch, position - 1, tHString.length - 1, 0); this.length += tHString.length;
}
},
strAssign: function(chars){
// for(var i = 0, len = chars.length; i < len; i++){
// this.ch[i] = chars[i];
// }
stringCopy(this.ch, chars, 0, chars.length - 1, 0);
this.length = chars.length;
},
strLength: function(){
return this.length;
},
strCompare: function(tHString){
for(var i = 0, len = this.length; i < len && i < tHString.length; i++)
if(this.ch[i] !== tHString.ch[i]) return this.ch[i] - tHString.ch[i]; return this.length - tHString.length;
},
clearString: function(){
this.ch = {};
this.length = 0;
},
concat: function(s){
var t = new HString(); // t.ch[0..this.length - 1] = this.ch[0..this.length - 1]
stringCopy(t.ch, this.ch, 0, this.length - 1, 0);
t.length = this.length + s.length;
// t.ch[this.length..t.length - 1] = s.ch[0..s.length - 1]
stringCopy(t.ch, s.ch, this.length, s.length - 1, 0); return t;
},
substring: function(position, len){
position = ~~position || 0;
len = ~~len || this.length;
if(position < 0 || position > this.length - 1 || len < 0 || len > this.length - position)
throw new Error('unexpected paramater'); var sub = new HString();
stringCopy(sub.ch, this.ch, 0, len - 1, position);
sub.length = len; return sub;
},
toString: function(){
var s = '';
for(var i = 0, len = this.length; i < len; i++){
s += this.ch[i];
}
return s;
}
}; function stringCopy(destination, target, destStart, length, targetStart){
destStart = destStart || 0;
length = length || target.length;
targetStart = targetStart || 0; for(var i = 0; i <= length; i++ ){
destination[destStart + i] = target[targetStart + i];
}
}
单元测试:
describe('HString tests', function(){
var s = new HString();
var t = new HString(); it('should assign chars', function(){
s.strAssign('hello world!');
expect(s + '').toBe('hello world!'); t.strAssign('jesus ');
expect(t + '').toBe('jesus ');
}); it('should insert string into s', function(){
s.strInsert(7, t);
expect(s + '').toBe('hello jesus world!');
}); it('should concat string', function(){
var ret = s.concat(t);
expect(ret + '').toBe('hello jesus world!jesus ');
}); it('should get substring', function(){
var ret = s.substring(0);
expect(ret + '').toBe('hello jesus world!'); ret = s.substring(5, 13);
expect(ret + '').toBe(' jesus world!'); ret = s.substring(3, 8);
expect(ret + '').toBe('lo jesus');
});
});
javascript实现数据结构:串--堆分配存储表示的更多相关文章
- javascript实现数据结构: 串的块链存储表示
和线性表的链式存储结构相类似,也可采用链式方式存储串值.由于串结构的特殊性--结构中的每个数据元素是一个字符,则用链表存储串值时,存在一个“结点大小”的问题,即每个结点可以存放一个字符,也可以存放多个 ...
- 串String(2):串的实现(堆分配存储表示法)
7/27/2017,先占个位,最近事情比较忙,明天敲一波代码,预测在一星期内搞定 9/02/2017,看到这个十分汗颜,八月去美帝学习了,没有抽空补上这一博文,计划这个月开了数据结构课后补上
- javascript实现数据结构与算法系列
1.线性表(Linear list) 线性表--简单示例及线性表的顺序表示和实现 线性表--线性链表(链式存储结构) 线性表的静态单链表存储结构 循环链表与双向链表 功能完整的线性链表 线性链表的例子 ...
- javascript实现数据结构:串--定长顺序存储表示以及kmp算法实现
串(string)(或字符串)是由零个或多个字符组成的有限序列.串中字符的数目称为串的长度.零个字符的串称为空串(null string),它的长度为零. 串中任意个连续的字符组成的子序列称为该串的子 ...
- 数据结构——串(KMP)
空串:长度为0的串 空格串:由一个或多个空格组成的串 串常用的3种机内表示方法: 定长顺序存储表示: 用一组地址连续的存储单元存储串的字符序列,每一个串变量都有一个固定长度的存储区,可用定长数组来描述 ...
- javascript实现数据结构:广义表
原文:javascript实现数据结构:广义表 广义表是线性表的推广.广泛用于人工智能的表处理语言Lisp,把广义表作为基本的数据结构. 广义表一般记作: LS = (a1, a2, ..., an ...
- javascript实现数据结构与算法系列:栈 -- 顺序存储表示和链式表示及示例
栈(Stack)是限定仅在表尾进行插入或删除操作的线性表.表尾为栈顶(top),表头为栈底(bottom),不含元素的空表为空栈. 栈又称为后进先出(last in first out)的线性表. 堆 ...
- JavaScript 版数据结构与算法(二)队列
今天,我们要讲的是数据结构与算法中的队列. 队列简介 队列是什么?队列是一种先进先出(FIFO)的数据结构.队列有什么用呢?队列通常用来描述算法或生活中的一些先进先出的场景,比如: 在图的广度优先遍历 ...
- JavaScript系列-----对象基于哈希存储(<Key,Value>之Value篇) (3)
JavaScript系列-----Objectj基于哈希存储<Key,Value>之Value 1.问题提出 在JavaScript系列-----Object之基于Hash<Key, ...
随机推荐
- Hadoop伪分布式搭建CentOS
所需软件及版本: jdk-7u80-linux-x64.tar.gz hadoop-2.6.0.tar.gz 1.安装JDK Hadoop 在需在JDK下运行,注意JDK最好使用Oracle的否则可能 ...
- Linux编译内核提示'make menuconfig' requires the ncurses libraries错误
原来使用的ubuntu 11.10系统由于误操作,导致系统崩溃,重新安装了ubuntu 11.10: 在编译内核的时候,提示如下错误: dingq@wd-u1110:~/hwsvn/2sw/1prj_ ...
- poj 2777 Count Color
题目连接 http://poj.org/problem?id=2777 Count Color Description Chosen Problem Solving and Program desig ...
- hdu 5327 Olympiad
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5327 Olympiad Description You are one of the competit ...
- Effective Objective-C 2.0之Note.02
1.多用类型常量,少用#define预处理指令 不要用预处理指令定义常量.这样定义出来的常量不含类型信息,编译器只是会在编译前据此执行查找与替换操作.即使有人重新定义了常量值,编译器也不会产生警告信息 ...
- func_num_args(),func_get_arg(),func_get_args()
<?php function testFunction1(){ return func_num_args(); } function testFunction2(){ return func_g ...
- STM32管教复用与重映射关系
摘自:http://blog.csdn.net/lincheng15/article/details/51789093 概括一下:复用就是一个引脚有几个功能,1.做普通IO输入输出 2.其他外设的输入 ...
- c++事件内核对象(event)进程间激活(转)
源出处:http://blog.csdn.net/richerg85/article/details/7538493 此文主要说明的是,c++中创建的一个事件内核对象可以在不同的程序(进程)间共用,也 ...
- Z_blog博客尝试 http://www.uuxin.com/
原来的博客由于没有备份所有数据全部丢失,很是郁闷. 又用Z-BLOG新建了一个博客.http://www.uuxin.com
- dmucs与distcc
之前配置distcc没有考虑负载均衡这一项,现在考虑使用dmucs实现distcc的负载均衡 官方手册 http://dmucs.sourceforge.net/ 使用官方手册编译会报错,等解决问题后 ...