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 ...
随机推荐
- cobbler搭建本地的yum仓库源
cobbler自动化安装参考文档 https://www.cnblogs.com/minseo/p/8537266.html 使用cobbler可以快速搭建一个本地的yum仓库 cobbler rep ...
- Django中,ajax检测注册用户信息是否可用?
ajax检测注册用户信息主体思路 1.在settings.py中配置需要使用的信息 #对static文件进行配置 STATICFILES_DIRS=[ os.path.join(BASE_DIR,'s ...
- vue中的iviewUI导出1W条列表数据每次只导出2000条的逻辑
导出弹窗的html <template> <Modal v-model="exportModal" width=400 :closable="false ...
- 初级ai思维导图,基础人工智能设计图
2017年2月8日09:35:46 仅供代码和逻辑设计图纸,只提供一个参考设计,后面可能会更新具体实施说明
- c++基本函数
std::abs 求绝对值函数 double abs (double x); float abs (float x); long double abs (long double x); std::sw ...
- vue 静态资源 压缩提交自动化
需要安装co和child_process模块,co可以执行多个promise,child_process可以执行命令行的库(cmd命令) 配置winrar(压缩包)坏境变量,参考资料https://j ...
- mysql 超时时间
小结: 1.mysql服务端主动关闭链接的秒数: MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variables https: ...
- 上传本地代码到github&&从github下载源码
最近在玩github,下面简单介绍下githup的使用 将本地代码同步到github. 使用Git GUI同步 1,先下载git,然后安装.右键如图所示. 2,在github里新建一个 reposi ...
- select下拉菜单实现通过数据库查询来设置默认值
查询网上各种资料要不比较难理解,要么有问题,现有一种简单通俗的理解方法 思路:读取数据库数据1,数据2需用到select选择菜单,但是又想每次查看是都显示读数据库的默认信息 demo: {% for ...
- ES6的十大新特性(转)
add by zhj: 该文章是由国外一哥们写的,由腾讯前端团队翻译,图片中的妹子长得挺好看的,很养眼,嘿嘿.我目前在学习ES6,这篇文章把ES6的 几个主要新特性进行了归纳总结,犹如脑图一般,让人看 ...