列表的实现-----数据结构与算法JavaScript描述 第三章
实现一个列表
script
var booklist = new List();
booklist.append('jsbook');
booklist.append('cssbook');
booklist.append('htmlbook');
console.log(booklist.length()) //3
console.log(booklist.toString()) //["jsbook", "cssbook", "htmlbook"]
TODO
这个列表又很多不完善的地方,比如
booklist.append(['cbook','c++book']) // 这样的就没合理的处理掉 ,以及去重判断等。
代码如下:
/**
* 列表的实现, 数据结构与算法JavaScript描述 第三章
* @constructor
*/
function List(){
this.listSize = 0;
this.pos = 0;
this.dataStore = []; //初始化一个空数组来保存列表元素
this.clear = clear;
this.find = find;
this.toString = toString;
this.insert = insert;
this.append = append;
this.remove = remove;
this.front = front;
this.end = end;
this.prev = prev;
this.next = next;
this.length = length;
this.currPos = currPos;
this.moveTo = moveTo;
this.getElement = getElement;
this.contains = contains;
//清空列表
function clear(){
delete this.dataStore;
this.dataStore.length = 0;
this.listSize = this.pos = 0;
}
//查找元素位置
function find(element){
for (var i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[i] == element) {
return i;
}
}
return -1;
}
//某元素之前插入元素
function insert(element,after){
var p = this.find(after)
if(p>-1){
this.dataStore.splice(p+1,0,element)
++this.listSize;
return true;
}
return false;
}
//插入元素
function append(element){
this.dataStore[this.listSize++] = element;
}
//移除元素
function remove(element){
var f = this.find(element)
if(f>-1){
this.dataStore.splice(f,1)
this.listSize--;
return true;
}
return false;
}
//把位置挪到最前面
function front(){
this.pos = 0;
}
//把位置挪到坐后面
function end(){
this.pos = (this.listSize-1)
}
//把位置上移一个节点
function prev(){
if (this.pos > 0) {
--this.pos;
}
}
//把位置下移一个节点
function next(){
if (this.pos < this.listSize-1) {
++this.pos;
}
}
//返回当前位置
function currPos(){
return this.pos;
}
//移到某一个位置
function moveTo(position){
this.pos = position;
}
//获得当前位置的元素
function getElement(){
return this.dataStore[this.pos]
}
//查找当前元素 是否在列表中
function contains(element){
for(var i=0;i<this.dataStore.length;i++){
if(this.dataStore[i]==element){
return true;
}
}
return false;
}
//tostring
function toString() {
return this.dataStore;
}
//返回该列表的长度
function length(){
return this.listSize;
}
}
列表的实现-----数据结构与算法JavaScript描述 第三章的更多相关文章
- 读书笔记《数据结构与算法JavaScript描述》第一章
第一章JavaScript的编程环境和模型 1.2JavaScript编程实践 1.2.1 声明和初始化变量 JavaScript中的变量默认为全局变量,如果初始化未被声明的变量,该变量就成了一个全局 ...
- 《数据结构与算法JavaScript描述》
<数据结构与算法JavaScript描述> 基本信息 作者: (美)Michael McMillan 译者: 王群锋 杜欢 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ISBN:9 ...
- 翻阅《数据结构与算法javascript描述》--数组篇
导读: 这篇文章比较长,介绍了数组常见的操作方法以及一些注意事项,最后还有几道经典的练习题(面试题). 数组的定义: JavaScript 中的数组是一种特殊的对象,用来表示偏移量的索引是该对象的属性 ...
- 数据结构与算法javascript描述
<数据结构与算法javascript描述>--数组篇 导读: 这篇文章比较长,介绍了数组常见的操作方法以及一些注意事项,最后还有几道经典的练习题(面试题). 数组的定义: JavaScri ...
- 《数据结构与算法JavaScript描述》中的一处错误
最近在看<数据结构与算法JavaScript描述>这本书,看到选择排序这部分时,发现一个比较大的错误. 原书的选择排序算法是这样的: function selectionSort() { ...
- 数据结构与算法 Javascript描述
数据结构与算法系列主要记录<数据结构与算法 Javascript描述>学习心得
- 读后感:数据结构与算法JavaScript描述
本书看完,对常见的数据结构与算法从概念上有了更深入的理解. 书中关于数组.栈和队列.链表.字典.散列.集合.二叉树.图.排序.检索.动态规划.贪心算法都有详细的介绍.算是一本不错的学习书籍. 栈和队列 ...
- 数据结构与算法JavaScript描述——列表
1.列表的抽象数据类型定义 2.实现列表类: 2.1 append:给列表添加元素: 2.2 remove: 从列表中删除元素: 2.3 find方法: 2.4 length:列表中有多少个元素: ...
- 数据结构与算法JavaScript描述——使用队列
1.使用队列:方块舞的舞伴分配问题 前面我们提到过,经常用队列模拟排队的人.下面我们使用队列来模拟跳方块舞的人.当 男男女女来到舞池,他们按照自己的性别排成两队.当舞池中有地方空出来时,选两个队 列中 ...
随机推荐
- ecshop的小总结
1 为防止非法调用自己的页面,在被包含页面加上: if (!defined('IN_ECS')) {die('Hacking attempt'); } 2 在主动包含页面要定义define('IN_ ...
- 关于Qt的灵异错误
今天用Qt编写了一个小程序,在运行的时候会报如下错误: ******SgAppenderImpl::SetImeSdkVer_begin ******SgAppenderImpl::SetImeSdk ...
- mit java open course assignment #4
package come; public class Library { // Add the missing implementation to this class String realLoca ...
- Python 操作 MySQL--(pymysql)
h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; back ...
- foj 2150 Fire Game(bfs暴力)
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M ...
- cocos2d-x for wp8 设置横竖屏
在主project文件(xxx.cpp , xxx为你的项目名)中, 函数名为void xxx::SetWindow(CoreWindow^ window) 相关代码片例如以下: <pre na ...
- fragment详解(官方文档)
原作者为: 苍山.感谢他分享的内容,现在分享出来给eoeAndroid的各位同胞. 详情参考http://www.eoeandroid.com/thread-71642-1-1.html和http:/ ...
- c++常用IDE
vs2013. CodeLite (c/c++/PHP) codeBlocks Qt Creator Xcode
- BZOJ 2726: [SDOI2012]任务安排( dp + cdq分治 )
考虑每批任务对后面任务都有贡献, dp(i) = min( dp(j) + F(i) * (T(i) - T(j) + S) ) (i < j <= N) F, T均为后缀和. 与j有关 ...
- STL MAP 反序迭代
ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...