链表: 每个元素,都有一个指针,指向下一个元素

		//链表
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模拟链表的更多相关文章

  1. js模拟链表---双向链表

    双向链表: 每个元素,有一个 next(指向下一个元素)和一个prev(指向前一个元素) function dbLinkedList(){ var length=0; var head = null; ...

  2. js模拟抛出球运动

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

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

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

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

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

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

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

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

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

  7. node.js模拟qq漂流瓶

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

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

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

  9. UVa12657 - Boxes in a Line(数组模拟链表)

    题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...

随机推荐

  1. webpack 配置

    https://segmentfault.com/a/1190000009454172

  2. 秒秒钟提高办公技巧的6个Excel技巧

    一.职工身份证号码是否登记重复(=IF(COUNTIF(B2:B13,B2&"*")>1,"重复","")) 职工列表人数众多 ...

  3. Windows的文件类型关联

    在用脚本语言开发时尤其是在windows环境下发现想自动在命令行环境下运行脚本必须要带着相应的解释器的路径才行,不然就会提示无法找到对应的命令,于是乎在<学习Ruby>这本书中对于文件类型 ...

  4. centos 7 部署 MQTT

    官方教程  :头痛的是nginx 和 mqtt布局有所冲突,后台不能使用需要调整,当然是用 nginx自家的布局没问题,但是要收费 官方地址 1.由于emqttd是用Erlang语言编写的,所以,在L ...

  5. 【每日一题】 uva-232 模拟+输出要求很严格

    https://cn.vjudge.net/problem/UVA-232 uva的题,结尾不能多\n,空格什么的 反正就是个中型模拟,题目多读就行 #define _CRT_SECURE_NO_WA ...

  6. GIS软件相关安装(持续更新)

    软件安装是GIS专业的必修课,总会忘记步骤,在此汇总 1.oracle ①无法登录 管理员登录 sqlplus sys/密码 as sysdba https://www.linuxidc.com/li ...

  7. Linear transformations. 线性变换与矩阵的关系

    0.2.2 Linear transformations. Let U be an n-dimensional vector space and let V be an m-dimensional v ...

  8. PHP之流程控制

    nest 嵌套 the curly braces 花括号 colon syntax 冒号语法 PHP三种if判断的写法 写法一: if(true){ }else if(){ }else if(){ } ...

  9. [daily][archlinux] 本地字符乱码, 无法显示中文

    一: 突然有一天,Konsole里边看见的中文文件名的文件,就变成了乱码.thunderbird存到本地的附件,文件名也变成了乱码. 在X下查看locale,内容如下: 手动设置了之后也不对. 但是在 ...

  10. SmokePing介绍

    一.SmokePing是什么? smokeping是rrdtool的作者Tobi Oetiker的作品,用Perl语言写的,主要是监视网络性能,所以它在图形显示方面有很大优势,也是一个很有特点的ope ...