JavaScript List

this.listSize = 0;
this.pos = 0;
this.dataSource = [];
this.clear = function() {
this.dataSource = [];
this.pos = 0;
this.listSize = 0;
};
this.find = function(item) {
for (var i = this.dataSource.length - 1; i >= 0; i--) {
if (this.dataSource[i] === item) {
return i;
}
};
return -1;
}
this.toString = function() {
return this.dataSource;
};
this.insert = function(item) {
var insertPos = this.find(item);
if (insertPos > -1) {
this.dataSource.splice(insertPos + 1, 0, item);
++this.listSize;
return true;
}
return false;
};
this.append = function(item) {
this.dataSource[this.listSize++] = item;
};
this.remove = function(item) {
var foundAt = this.find(item);
if (foundAt > -1) {
this.dataSource.splice(foundAt, 1);
--this.listSize;
return true;
}
return false;
};
this.front = function() {
this.pos = 0;
};
this.end = function() {
this.pos = this.listSize - 1;
};
this.prev = function() {
if (this.pos > 0) {
--this.pos;
}
};
this.next = function() {
if (this.pos < this.listSize - 1) {
++this.pos;
}
};
this.length = function() {
return this.listSize;
};
this.currPos = function() {
return this.pos;
};
this.moveTo = function(position) {
this.pos = position;
};
this.getElement = function() {
return this.dataSource[this.pos];
};
this.contains = function(item) {
for (var i = this.dataSource.length - 1; i >= 0; i--) {
if (this.dataSource[i] === item) {
return true;
}
};
return false;
};
}
var list = new List();
list.append("shidengyun");
list.append("zhujing");
list.append("shidengyun");
list.append("zhujing");
console.log(list.toString());
console.log('-------------');
list.insert("shidengyun");
console.log(list.toString());
console.log('-------------');
list.remove("zhujing");
console.log(list.toString());
console.log('-------------');
list.front();
console.log(list.getElement());
list.next();
console.log(list.getElement());
list.prev();
console.log(list.getElement());
console.log('-------------');
for (list.front(); list.currPos < list.length(); list.next()) {
console.log(list.getElement());
}
console.log('-------------');
for (list.end(); list.currPos >= 0; list.prev()) {
console.log(list.getElement());
}
JavaScript List的更多相关文章
- JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议
软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...
- javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈
Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...
- Javascript 的执行环境(execution context)和作用域(scope)及垃圾回收
执行环境有全局执行环境和函数执行环境之分,每次进入一个新执行环境,都会创建一个搜索变量和函数的作用域链.函数的局部环境不仅有权访问函数作用于中的变量,而且可以访问其外部环境,直到全局环境.全局执行环境 ...
- 探究javascript对象和数组的异同,及函数变量缓存技巧
javascript中最经典也最受非议的一句话就是:javascript中一切皆是对象.这篇重点要提到的,就是任何jser都不陌生的Object和Array. 有段时间曾经很诧异,到底两种数据类型用来 ...
- 读书笔记:JavaScript DOM 编程艺术(第二版)
读完还是能学到很多的基础知识,这里记录下,方便回顾与及时查阅. 内容也有自己的一些补充. JavaScript DOM 编程艺术(第二版) 1.JavaScript简史 JavaScript由Nets ...
- 《Web 前端面试指南》1、JavaScript 闭包深入浅出
闭包是什么? 闭包是内部函数可以访问外部函数的变量.它可以访问三个作用域:首先可以访问自己的作用域(也就是定义在大括号内的变量),它也能访问外部函数的变量,和它能访问全局变量. 内部函数不仅可以访问外 ...
- JavaScript权威指南 - 函数
函数本身就是一段JavaScript代码,定义一次但可能被调用任意次.如果函数挂载在一个对象上,作为对象的一个属性,通常这种函数被称作对象的方法.用于初始化一个新创建的对象的函数被称作构造函数. 相对 ...
- JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome
今天为大家分享一下我自己制作的浏览器滚动条,我们知道用css来自定义滚动条也是挺好的方式,css虽然能够改变chrome浏览器的滚动条样式可以自定义,css也能够改变IE浏览器滚动条的颜色.但是css ...
- JavaScript进阶之路(一)初学者的开始
一:写在前面的问题和话 一个javascript初学者的进阶之路! 背景:3年后端(ASP.NET)工作经验,javascript水平一般般,前端水平一般般.学习资料:犀牛书. 如有误导,或者错误的地 ...
- 梅须逊雪三分白,雪却输梅一段香——CSS动画与JavaScript动画
CSS动画并不是绝对比JavaScript动画性能更优越,开源动画库Velocity.js等就展现了强劲的性能. 一.两者的主要区别 先开门见山的说说两者之间的区别. 1)CSS动画: 基于CSS的动 ...
随机推荐
- 【LeetCode】前缀树 trie(共14题)
[208]Implement Trie (Prefix Tree) (2018年11月27日) 实现基本的 trie 树,包括 insert, search, startWith 操作等 api. 题 ...
- opencv 环境配置-windowsx64 +VS2017
opencv版本为4.1.1官方最新版本, https://sourceforge.net/projects/opencvlibrary/ 先配置本地工程环境: 右键我的电脑-属性 将bin的路径添加 ...
- Flask之路由相关
1.装饰器中的参数 @app.route("/info", methods=["GET", "POST"]) def student_inf ...
- Linux必备软件安装
若想Linux体验感更强,安装linux著名的KDE界面 (里面可以配置很多个性化的界面,特效等) # apt install plasma-desktop mpv ---很炫酷的视频播放器okula ...
- Python之批量读取文件【面试必学】
python的os模块可以实现普遍的操作系统功能,并且和平台无关.以下为实现根目录下文件的批量读取. os.listdir(dirname)可以列出dirname下的目录和文件,依次读取相应的文件即可 ...
- 【leetcode】1053. Previous Permutation With One Swap
题目如下: Given an array A of positive integers (not necessarily distinct), return the lexicographically ...
- Influxdb根据配置文件启动(Influxdb的数据存储)
1.在Influxdb文件夹下建立一个bat文件 2.文件内容如下: @echo offSETLOCAL :: 获取当前批处理所在路径SET InfluxdP==%~dp0 :: 开启influxdb ...
- Xcode出现报错,但是没有给出详细信息,可以看这里
Xcode出现报错,"Xcode build:clang: error: linker command failed with exit code 1 (use -v to..." ...
- Python3解leetcode Average of Levels in Binary Tree
问题描述: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...
- 【Java】JSONObject学习
介绍 JSONObject只是一种数据结构,可以理解为JSON格式的数据结构(key-value 结构),可以使用put方法给json对象添加元素.JSONObject可以很方便的转换成字符串,也可以 ...