双向链表: 每个元素,有一个 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模拟链表---双向链表的更多相关文章

  1. js模拟链表

    链表: 每个元素,都有一个指针,指向下一个元素 //链表 function LinkedList(){ var head = null; length = 0; this.append = funct ...

  2. Codeforces Round #552 (Div. 3) E. Two Teams (模拟,优先队列,双向链表)

    题意:有\(n\)个队员站成一排,有两个教练分别选人,每次选当前剩余人中的能力值最大的那个以及他两边相邻的\(k\)个人,问最后每个人所在队伍情况. 题解:优先队列模拟,以及双向链表,先用结构体存入每 ...

  3. js模拟抛出球运动

    js练手之模拟水平抛球运动 -匀加速运动 -匀减速运动 模拟运动有些基本的思路,当前所在点的坐标,元素的长宽是多少,向右/向下运动x/y增加,向上/向左运动x/y减少,运动的路程是多少,用什么方程进行 ...

  4. Gremlins.js – 模拟用户随机操作的 JS 测试库

    Gremlins.js 是基于 JavaScript 编写的 Monkey 测试库,支持 Node.js 平台和浏览器中使用.Gremlins.js 随机模拟用户操作:单击窗口中的任意位置,在表格中输 ...

  5. hdu5009 Paint Pearls (DP+模拟链表)

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...

  6. JS 模拟手机页面文件的下拉刷新

    js 模拟手机页面文件的下拉刷新初探 老总说需要这个功能,好吧那就看看相关的东西呗 最后弄出了一个简单的下拉刷新页面的形式,还不算太复杂 查看 demo 要在仿真器下才能看到效果,比如chrome的里 ...

  7. 由chrome剪贴板问题研究到了js模拟鼠标键盘事件

    写在前面 最近公司在搞浏览器兼容的事情,所有浏览器兼容的问题不得不一个人包了.下面来说一下今天遇到的一个问题吧 大家都知道IE下面如果要获得剪贴板里面的信息的话,代码应该如下所示 window.cli ...

  8. node.js模拟qq漂流瓶

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) node.js模拟简易漂流瓶,页面有扔瓶子和捡瓶子的功能,一个瓶子只能被捡到一次,阅读完就置状态位, ...

  9. css配合js模拟的select下拉框

    css配合js模拟的select下拉框 <!doctype html> <html> <head> <meta charset="utf-8&quo ...

随机推荐

  1. 免费的SSL证书(LINUX)

    贫穷限制了我的SSL. 说起来也简单,免费的SSL证书授权机构,我使用的是Certbot 选择服务器开启的服务,像我php之流,无非apache和nginx,然后选择使用的服务器类型.嗯,补充一句,这 ...

  2. ssh 管理 linux登录远程服务器

    使用 ssh 免秘登录方式 客户端:1. 生成公钥和私钥 ssh-keygen 一般不需要对私钥设置口令(passphrase),如果担心私钥的安全,这里可以设置一个. 运行结束以后,在$HOME/. ...

  3. MSDN2001 快捷方式丢失的处理方法

    1.  C:\Windows\hh.exe,右键 - 发送快捷方式到桌面 2. 给快捷方式改名为:MSDN2001 3. 右键快捷方式 - 属性- 快捷方式选项卡中的目标填写: C:\Windows\ ...

  4. 别致的语言GO(GO语言初涉)

    最近由于各种原因(好吧,其实是犯懒)已经许久没有再写新的博文了!最近正好在学习一门新的语言,所以正好记录一下自己的学习成果!最近利用每天晚上下班回来后的几小时,学习了Google开发的Go语言,算是对 ...

  5. pycharm更新之后显示问题

    pycharm更新之后显示问题 在新版pycharm中等号和其他符号会连在一块,下面是解决方法 添加公众号:

  6. Flask web开发之路五

    Jinjia2模板 紧接着上篇博客,接下去写if条件判断和for循环遍历以及过滤器 if条件判断 主app文件代码: from flask import Flask,render_template a ...

  7. Ubuntu上pip安装uwsgi失败的原因之一(未联网)

    ubuntu@ubuntu:~$ sudo pip install uwsgi 报错:The directory '/home/ubuntu/.cache/pip/http' or its paren ...

  8. python-Beautiful rose

    热爱python,热爱生活,python需要浪漫,让我带大家走进浪漫的国度...写的不好的地方请大佬指教 import turtle import time class Rose: def __ini ...

  9. [No000018E]Vim快速跳转任意行、任意列以及高亮显示当前行、当前列方法-Vim使用技巧(3)

    vim提供了丰富的快速跳转任意行.任意列的方法,方便高效地移动光标,定位文件位置. 一.Vim行跳转 使用vim查看文件时,使用以下命令可以快速跳转文件首.尾行,方便对整个文件有个全局把握. 1.1 ...

  10. tensorflow scope的作用

    我们在使用tensorflow的时候,当你想复用一个函数的模块,调试时候回提示你变量已经出现,提示你是否重用.那我们当然是不重用的,因为每一个变量都是我们需要的. 要体现不同,就在不同的变量中使用na ...