js模拟链表---双向链表
双向链表: 每个元素,有一个 next(指向下一个元素)和一个prev(指向前一个元素)
function dbLinkedList(){
var length=0;
var head = null;
var tail = null;
function getNode(ele){
return {
ele:ele,
next:null,
prev:null
}
}
this.insert = function(pos,ele){
if(pos >= 0 && pos <= length){
var nodes = getNode(ele);
var current = head;
var previous,index=0;
if(pos == 0){
if(!head){
head = nodes;
tail = nodes;
}else{
nodes.next = current;
current.prev = nodes;
head = nodes;
}
}else if(pos == length){
current = tail;
current.next = nodes;
nodes.prev = current;
tail = nodes;
}else{
while (index++ < pos){
previous = current;
current = current.next;
}
nodes.next = current;
previous.next = nodes;
current.prev = nodes;
nodes.prev = previous;
}
length++;
return true;
}else{
return false;
}
}
this.append = function(ele){
return this.insert(length,ele);
}
this.removeAt = function(pos){
if(pos > -1 && pos < length){
var current = head,
previous,index=0;
//移除第一项
if(pos == 0){
head = current.next;
//如果只有一项,更新tail
if(length == 1){
tail = null;
}else{
head.prev = null;
}
}else if(pos == length-1){
current = tail;
tail = current.prev;
tail.next = null;
}else{
while(index++ < pos){
previous = current;
current = current.next;
}
//前面的next,指向当前项的next,即干掉当前项
previous.next = current.next;
current.next.prev = previous;
}
length--;
return current.ele;
}else{
return null;
}
};
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 dbLinkedList();
list.append("a");
list.append("b");
list.append("c");
list.insert(2,"bgb");
list.append("d");
list.append("大大");
list.toString();
list.remove("c");
list.toString();
js模拟链表---双向链表的更多相关文章
- js模拟链表
链表: 每个元素,都有一个指针,指向下一个元素 //链表 function LinkedList(){ var head = null; length = 0; this.append = funct ...
- Codeforces Round #552 (Div. 3) E. Two Teams (模拟,优先队列,双向链表)
题意:有\(n\)个队员站成一排,有两个教练分别选人,每次选当前剩余人中的能力值最大的那个以及他两边相邻的\(k\)个人,问最后每个人所在队伍情况. 题解:优先队列模拟,以及双向链表,先用结构体存入每 ...
- 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 ...
随机推荐
- I - The 3n + 1 problem(2.4.2)
I - The 3n + 1 problem(2.4.2) Crawling in process... Crawling failed Time Limit:1000MS Memory Li ...
- shell特殊变量,记录一下
$0 当前脚本的文件名 $n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个参数是$1,第二个参数是$2. $# 传递给脚本或函数的参数个数. $* 传递给脚本或函数的所有参数. ...
- tomcat项目启动报错java.lang.ClassCastException: org.apache.xerces.parsers.XML11Configuration……
周一上班启动项目,报错如图: 看到网上说的原因,有jar包冲突造成的,我这里的是: 情况:console打印信息有多次连接数据库,但该项目只需要连接一个库.再仔细看,发现有其他项目的信息,打开tomc ...
- 对iphone手机IMU的陀螺仪、加速度计、图像的时间戳做对齐处理
https://blog.csdn.net/chishuideyu/article/details/77479758 加速度计和陀螺仪的时间戳一致 ros 的message filters可以做时间同 ...
- [No000016C]做企业分析的三个重要工具
个工具>这本书,内容很基础,但是逻辑很清晰.里面介绍了三个常用的分析方法:pest分析法.波特五力模型以及swot分析法,为脑袋里没有分析思路的人提供了分析框架. 故将这三个分析方法做成了思维导 ...
- [No0000E1]C# 关键字
关键字是 C# 编译器预定义的保留字.这些关键字不能用作标识符,但是,如果您想使用这些关键字作为标识符,可以在关键字前面加上 @ 字符作为前缀. 在 C# 中,有些标识符在代码的上下文中有特殊的意义, ...
- mysql 超时时间
小结: 1.mysql服务端主动关闭链接的秒数: MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variables https: ...
- 深入hash
hash真的很好用,这些杂一点的知识点我觉得还是很有必要的,对还有离散化. 1<=N<=1,000,000,其它所有数据都在[0...1,000,000,000]范围内 看起来很简单一道水 ...
- Tacacs+认证详细调研
1 .TACACS+概述 1.1 什么是TACACS+ TACACS+(Terminal Access Controller Access Control System,终端访问控制器控制系统协议 ...
- 第二章 深入C#数据类型
深入C#数据类型 巧记:值(无ref)+值=不变 值(无ref)+引=变 引(有ref)+值/引=变 1.值类型和引用类型 1.引用类型 源于system.object家族 ...