双向链表: 每个元素,有一个 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. 关于Dosbox0.74无法使用masm命令

    今天尝试在dosbox里编译asm源代码文件 但是提示“illegal command”,也就是非法命令 开始还以为我的dosbox版本不对 但是去网上查阅资料发现别人用这个版本都可以使用 所以百思不 ...

  2. cc2650 7x7封装更换为 5X5 4x4

    https://www.deyisupport.com/question_answer/wireless_connectivity/bluetooth/f/103/t/104028.aspx 解决方案 ...

  3. day0321 生成器

    一.生成器 1.迭代器: 1.1.调用方法直接返回 1.2.可迭代对象通过执行iter方法得到 迭代器的优势:节省内存. 2.生成器:有些情况我们也需要也需要节省空间,只能是自己写来实现迭代器的功能就 ...

  4. [administrative][CentOS] 新装系统时如何正确精准的选择基础环境和软件包

    出于不同的目的,在进行全新CentOS安装的时候,我们到底应该如何作出选择. 是mininal,base server, base web server, 还是啥? 答案在这里: https://ac ...

  5. CABasicAnimation 划线动画

    CGFloat animateDuration = ; UIBezierPath *bezierPath = [[UIBezierPath alloc] init]; CGPoint centerFr ...

  6. 过滤器会拦截 前端页面加载 js文件的请求

    学艺不精啊.....之前就总结过博客: JAVA中解决Filter过滤掉css,js,图片文件等问题 结果现在又犯了老错误~ 情况如下: index.jsp 页面的验证码输入栏绑定了异步验证(jQur ...

  7. LeetCode 427 Construct Quad Tree 解题报告

    题目要求 We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be tru ...

  8. LeetCode 682 Baseball Game 解题报告

    题目要求 You're now a baseball game point recorder. Given a list of strings, each string can be one of t ...

  9. idea tomcat控制台system.out.println是乱码

    配置一下tomcat的信息.然后设置VM options.添加:-Dfile.encoding=UTF-8

  10. 【Python全栈-HTML】HTML入门

    HTML入门介绍 参考: https://www.bilibili.com/video/av21663728/?p=339 http://www.cnblogs.com/yuanchenqi/arti ...