堆分配存储表示

这种存储表示的特点是,仍以一组地址连续的存储单元存放串值字符序列,但它们的存储空间是在程序执行过程中动态分配而得。

结构图:

实现:

 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实现数据结构:串--堆分配存储表示的更多相关文章

  1. javascript实现数据结构: 串的块链存储表示

    和线性表的链式存储结构相类似,也可采用链式方式存储串值.由于串结构的特殊性--结构中的每个数据元素是一个字符,则用链表存储串值时,存在一个“结点大小”的问题,即每个结点可以存放一个字符,也可以存放多个 ...

  2. 串String(2):串的实现(堆分配存储表示法)

    7/27/2017,先占个位,最近事情比较忙,明天敲一波代码,预测在一星期内搞定 9/02/2017,看到这个十分汗颜,八月去美帝学习了,没有抽空补上这一博文,计划这个月开了数据结构课后补上

  3. javascript实现数据结构与算法系列

    1.线性表(Linear list) 线性表--简单示例及线性表的顺序表示和实现 线性表--线性链表(链式存储结构) 线性表的静态单链表存储结构 循环链表与双向链表 功能完整的线性链表 线性链表的例子 ...

  4. javascript实现数据结构:串--定长顺序存储表示以及kmp算法实现

    串(string)(或字符串)是由零个或多个字符组成的有限序列.串中字符的数目称为串的长度.零个字符的串称为空串(null string),它的长度为零. 串中任意个连续的字符组成的子序列称为该串的子 ...

  5. 数据结构——串(KMP)

    空串:长度为0的串 空格串:由一个或多个空格组成的串 串常用的3种机内表示方法: 定长顺序存储表示: 用一组地址连续的存储单元存储串的字符序列,每一个串变量都有一个固定长度的存储区,可用定长数组来描述 ...

  6. javascript实现数据结构:广义表

    原文:javascript实现数据结构:广义表  广义表是线性表的推广.广泛用于人工智能的表处理语言Lisp,把广义表作为基本的数据结构. 广义表一般记作: LS = (a1, a2, ..., an ...

  7. javascript实现数据结构与算法系列:栈 -- 顺序存储表示和链式表示及示例

    栈(Stack)是限定仅在表尾进行插入或删除操作的线性表.表尾为栈顶(top),表头为栈底(bottom),不含元素的空表为空栈. 栈又称为后进先出(last in first out)的线性表. 堆 ...

  8. JavaScript 版数据结构与算法(二)队列

    今天,我们要讲的是数据结构与算法中的队列. 队列简介 队列是什么?队列是一种先进先出(FIFO)的数据结构.队列有什么用呢?队列通常用来描述算法或生活中的一些先进先出的场景,比如: 在图的广度优先遍历 ...

  9. JavaScript系列-----对象基于哈希存储(<Key,Value>之Value篇) (3)

    JavaScript系列-----Objectj基于哈希存储<Key,Value>之Value 1.问题提出 在JavaScript系列-----Object之基于Hash<Key, ...

随机推荐

  1. USB协议分析

    一.USB设备描述结构 1.逻辑组织结构 在USB设备的逻辑组织中,包含设备.配置.接口和端点4个层次.设备通常有一个或多个配置,配置通常有一个或多个接口,接口有零或多个端点. 每个USB设备都可以包 ...

  2. USB总线介绍

    •USB 1.0出现在1996年的,速度只有1.5Mb/s1998年升级为USB 1.1,速度也提升到12Mb/s,称之为”full speed” •USB2.0规范是由USB1.1规范演变而来的.它 ...

  3. CMD怎样建立文件?

    一.建立空文件的几种方法1.cd.>a.txtcd.表示改变当前目录为当前目录,即等于没改变:而且此命令不会有输出.>表示把命令输出写入到文件.后面跟着a.txt,就表示写入到a.txt. ...

  4. SQL语句基础之 管理数据库,表 和 数据

    MySQL中的基本sql语句 MySQL中主要有三个大的对象,第一个是数据库,有了数据库后,我们才能在数据库里面建表,因为Mysql是关系数据库,它的数据都会以记录的形式存到表里,所以第二个是表,然后 ...

  5. Linux 配置 vimrc

    由于熟悉了Windows下利用编译器进行编程,所以在刚刚接触Linux后的编程过程中会感觉其vim编译器的各种不方便编写程序,在逐渐的学习过程中了解到可以通过配置vimrc使得vim编译时类似于VS. ...

  6. 在mac系统安装Apache Tomcat的详细步骤[转]

    对于Apache Tomcat 估计很多童鞋都会,那么今天就简单说下在mac上进行tomcat的安装: 第一步:下载Tomcat   这里Himi下载的tomcat version:7.0.27 直接 ...

  7. python中的各种排序

    #encoding=utf-8 import random from copy import copy def directInsertSort(seq): """ 直接 ...

  8. 三星N8000/N8010通用刷机教程

    前面已经讲到过如何给三星n8000/n8010 Galaxy Note 10.1获取ROOT权限了.接下来就顺便告诉大家怎么给三星n8000/n8010刷机吧.其实给三星n8000/n8010刷机过程 ...

  9. having count group by

    select count(*) from (select field2,count(field2) from bsgj.table1  group by field,items_id having(c ...

  10. [转]ubuntu错误解决E: Sub-process /usr/bin/dpkg returned an error code (1)

    [转]ubuntu错误解决E: Sub-process /usr/bin/dpkg returned an error code (1) http://yanue.net/post-123.html ...