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

		//链表
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. redis(一)--认识redis

    Redis官网对redis的定义是:“Redis is an open source, BSD licensed, advanced key-value cache and store”,可以看出,R ...

  2. easyui---editgrid

    on 点击新增用户,不是弹出一个一个dialog,而是直接在表格下面增加一行可编辑的,然后点击保存就可以新增 第一步:加一个toolbar,在handler中当点击新增用户,会调用datagrid的a ...

  3. 项目报错java.lang.ClassNotFoundException: org.common.SessionListener

    现象:项目报错java.lang.ClassNotFoundException: org.common.SessionListener,并且myeclipse左侧Package Explorer中项目 ...

  4. 洛谷 P1208混合牛奶【贪心】

    题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是 ...

  5. MS14-064 漏洞测试入侵——20145301

    MS14-064 漏洞测试入侵 Microsoft Windows OLE远程代码执行漏洞,OLE(对象链接与嵌入)是一种允许应用程序共享数据和功能的技术 执行摘要 此安全更新可解决 Microsof ...

  6. PCI 设备调试手段

    Author: Younix Platform: RK3399 OS: Android 6.0 Kernel: 4.4 Version: v2017.04 一PCI 设备调试手段 busybox ls ...

  7. redis有序集合性能 列表、集合、有序集合

    https://www.cnblogs.com/pirlo21/p/7120935.html 1.1 列表 列表(list)类型是用来存储多个字符串,元素从左到右组成一个有序的集合.列表中的每个字符串 ...

  8. DBCHART直方图顶端显示数字

    双击DBCHART-->选SERIES选项卡-->选MARKS-->选STYLE值为VALUE

  9. day1_jmeter接口测试

    一.Jmeter-http接口脚本: 步骤: 1.添加线程组 2.添加http请求 3.在http请求中写入接口url.路径.请求方式.参数 4.添加查看结果树 5.调用接口.查看返回值 二.Jmet ...

  10. ECharts图形库

    ECharts图形库百度的项目,图形的创建也比较简单,直接引用Javascript即可 1,引入<script src="{{ url_for("static",f ...