数据结构和算法 (javascript版本)之列表
来源:互联网 发布:蔡英文2017 知乎 编辑:程序博客网 时间:2024/06/05 22:31
列表
概述
日常生活中,人们经常使用列表:待办事项列表、购物清单、歌词列表、歌曲列表等
注意:如果数据结构非常复杂那么列表的意义就不大了
1.列表的抽象
具体代码实现
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 find(element){ for (var i = 0; i < this.dataStore.length; i++) { if(this.dataStore[i] ==element){ return i; } } return -1; } function append(element){ this.dataStore[this.listSize++] = element; } function remove(element){ var foundAt = this.find(element); if(foundAt > -1){ this.dataStore.splice(foundAt,1); --this.listSize; return true; } return false; } function length(){ return this.listSize; } function toString(){ return this.dataStore; } function insert(element,after){ var insertPos = this.find(element); if(insertPos > -1){ this.dataStore.splice(insertPos,0,element); ++this.listSize; return true; } return false; } function clear(){ delete this.dataStore; this.dataStore = []; this.listSize = this.pos= 0; } function contains(element){ for (var i = 0; i < this.dataStore.length; i++) { if(this.dataStore[i] == element){ 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]; }}/*var movies = read('movies.txt');alert(movies);*/
0 0
- 数据结构和算法 (javascript版本)之列表
- 数据结构与算法JavaScript - 列表
- 数据结构和算法(Java版本)
- javascript数据结构和算法
- 《数据结构和算法分析》学习之散列表
- 数据结构和算法JavaScript描述
- 数据结构与算法之五 链接列表
- 算法及数据结构之散列表
- 数据结构与算法分析之散列表
- python数据结构之列表和元组(list/tuple)
- 数据结构和算法(01)之综述
- 可视化数据结构和算法中文版本实现
- 学习JavaScript数据结构与算法(七)——散列表(一)
- 学习JavaScript数据结构与算法(七)——散列表(二)
- 《数据结构》之(散列表)
- 第三章:数据结构与算法javascript描述: 列表
- JavaScript描述数据结构与算法——列表
- 数据结构与算法JavaScript描述[第三章](列表)
- java中的注解
- SpringMVC4+Hibernate4 问题汇总
- Xcode 真机调试中"There was an internal API error"错误解决方法
- Android SharedPreferences保存/获取简单本地数据
- max_heap_table_size 和 tmp_table_size
- 数据结构和算法 (javascript版本)之列表
- JSPatch实现原理详解
- 前谷歌首席 Java 架构师谈如何设优秀的 API
- static const
- 求最长回文子串
- getAttribute()与getParameter的区别
- A request has been denied as a potential CSRF attack.”
- C++指针
- TortoiseSVN webstorm命令行