js模拟链表
链表: 每个元素,都有一个指针,指向下一个元素
//链表
function LinkedList(){
var head = null;
length = 0; this.append = function(ele){
var cnode = {
ele:ele,
next:null
};
if(head === null){
head = cnode;
}else{
//追加到节点
var current = head;
while(current.next){
current = current.next;
}
current.next = cnode;
}
length++;
}; this.removeAt = function(pos){
//删除第几个元素
//检查越界
if(pos > -1 && pos < length){
var current = head,
previous,
index = 0;
//移除第一项
if(pos == 0){
head = current.next;
}else{
while(index++ < pos){
previous = current;
current = current.next;
}
//前面的next,指向当前项的next,即干掉当前项
previous.next = current.next;
}
length--;
return current.ele;
}else{
return null;
}
};
this.insert = function(pos,ele){
if(pos >= 0 && pos <= length){
var nodes = {
ele:ele,
next:null
};
var current = head,
previous,
index = 0;
if(pos == 0){
//第一项插入
nodes.next = current;
head = nodes;
}else{
while(index++ < pos){
previous = current;
current = current.next;
}
nodes.next = current;
previous.next = nodes;
}
length++;
return true;
}else{
return false;
}
};
this.indexOf = function (ele){
var current = head;
var index = -1;
while(current){
index++;
if(current.ele === ele){
return index;
}
current = current.next;
}
return -1;
}; this.remove = function(ele){
var index = this.indexOf(ele);
return this.removeAt(index);
};
this.toString = function(){
var current = head;
var str = '';
var index = 0;
while(current){
str = str + current.ele+"-" + index + "\n";
index++;
current = current.next;
}
console.log(str);
};
this.size = function(){
return length;
};
this.isEmpty = function(){
return !length;
}
this.getHead = function(){
return head;
} } var list = new LinkedList();
list.append("a");
list.append("b");
list.append("c");
list.insert(2,"bgb");
list.append("d");
list.append("大大");
list.toString();
list.remove("c");
js模拟链表的更多相关文章
- js模拟链表---双向链表
双向链表: 每个元素,有一个 next(指向下一个元素)和一个prev(指向前一个元素) function dbLinkedList(){ var length=0; var head = null; ...
- js模拟抛出球运动
js练手之模拟水平抛球运动 -匀加速运动 -匀减速运动 模拟运动有些基本的思路,当前所在点的坐标,元素的长宽是多少,向右/向下运动x/y增加,向上/向左运动x/y减少,运动的路程是多少,用什么方程进行 ...
- Gremlins.js – 模拟用户随机操作的 JS 测试库
Gremlins.js 是基于 JavaScript 编写的 Monkey 测试库,支持 Node.js 平台和浏览器中使用.Gremlins.js 随机模拟用户操作:单击窗口中的任意位置,在表格中输 ...
- hdu5009 Paint Pearls (DP+模拟链表)
http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...
- JS 模拟手机页面文件的下拉刷新
js 模拟手机页面文件的下拉刷新初探 老总说需要这个功能,好吧那就看看相关的东西呗 最后弄出了一个简单的下拉刷新页面的形式,还不算太复杂 查看 demo 要在仿真器下才能看到效果,比如chrome的里 ...
- 由chrome剪贴板问题研究到了js模拟鼠标键盘事件
写在前面 最近公司在搞浏览器兼容的事情,所有浏览器兼容的问题不得不一个人包了.下面来说一下今天遇到的一个问题吧 大家都知道IE下面如果要获得剪贴板里面的信息的话,代码应该如下所示 window.cli ...
- node.js模拟qq漂流瓶
(文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) node.js模拟简易漂流瓶,页面有扔瓶子和捡瓶子的功能,一个瓶子只能被捡到一次,阅读完就置状态位, ...
- css配合js模拟的select下拉框
css配合js模拟的select下拉框 <!doctype html> <html> <head> <meta charset="utf-8&quo ...
- UVa12657 - Boxes in a Line(数组模拟链表)
题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...
随机推荐
- rxjs 常用的静态操作符
操作符文档 API create const { Observable } = require('rxjs'); // 创建 Observables var observable = Observab ...
- 引用了System.Configuration命名空间,却找不到ConfigurationManager类
用ConfigurationManager类来读取应用程序配置文件的信息时,提示:System.Configuration命名空间下找不到ConfigurationManager类 查过资料后得知:要 ...
- Lucene.net(4.8.0) 学习问题记录六:Lucene 的索引系统和搜索过程分析
前言:目前自己在做使用Lucene.net和PanGu分词实现全文检索的工作,不过自己是把别人做好的项目进行迁移.因为项目整体要迁移到ASP.NET Core 2.0版本,而Lucene使用的版本是3 ...
- 洛谷P1162 填涂颜色【bfs】
题目链接:https://www.luogu.org/problemnew/show/P1162 题意: 有一个0和1组成的矩阵,一些1组成一个闭合圈,圈住一些0,现在要把被圈住的这些0变成2输出. ...
- 英语专业出身也要走向python
这两年一直徘徊在学习python和放弃python的道路上不断的徘徊,今年终于没有在蹉跎下去,选择了开始新的自我挑战,零基础开始学习python. 作为一名英语专业毕业的文科生,学习编程还是相对有些困 ...
- [No0000141]Outlook,设置全局已读回执
Outlook,设置全局已读回执 文件->选项
- 配置llama实现impala on yarn-验证未通过,仅以此文作为参考
以下内容采自网络,目前验证未通过,仅以此作为参考: 简介:早期的Impala版本中,为了使用Impala,我们通常会在以Client/Server的结构在各个集群节点启动impala-server.i ...
- day 0313函数的初识
1.函数的定义: 定义:def 关键词开头,空格之后接函数名和圆括号(),还有最后一个‘:’ def是固定的,定义函数的关键字. 空格-是为了将关键字和函数名分开,必须有的. 函数名:只能包括字符串, ...
- OWA (Office Web Access)
exchange的web网页,可以enrich的打开,用起来还行outlook一样. 同事的chrome(under windows) 默认就是i这样的.也没装插件,也没有怎样. 我的chrome(u ...
- 彻底卸载tv
1.卸载 2.C:\Program Files (x86),找到teamviewer选项,右击删除 3.开始--输入regedit,打开注册表,找到如下路径:HKEY_LOCAL_MACHINE\SO ...