function LinkedList() {
var Node = function(element) {
this.element = element;
this.next = null
}
var length = 0;
var head = null;
this.append = function(element) {
var node = new Node(element),
current;
if (head == null) {
head = node
} else {
current = head;
while (current.next) {
current = current.next
}
current.next = node
}
length++
}
this.insert = function(position, element) {
if (position >= 0 && position <= length) {
var node = new Node(element),
current = head,
previous,
index = 0;
if (position == 0) {
node.next = current;
head = node
} else {
while (index++<position) {
previous = current;
current = current.next
}
node.next = current;
previous.next = node
}
length++;
return true
} else {
return false
}
}
this.removeAt = function(position) {
if (position > -1 && position < length) {
var current = head,
previous, index = 0;
if (position == 0) {
head = current.next
} else {
while (index++<position) {
previous = current;
current = current.next
}
previous.next = current.next
}
length--;
return current.element
} else {
return null
}
}
this.remove = function(element) {
var index = this.indexOf(element);
return this.removeAt(index)
}
this.indexOf = function(element) {
var current = head,
index = -1;
while (current) {
if (element == current.element) {
return index
}
index++;
current = current.next
}
return - 1
}
this.isEmpty = function() {
return length == 0
}
this.size = function() {
return length
}
this.getHead = function() {
return head
}
this.toString = function() {
var current = head,
string = '';
while (current.next) {
string = current.element;
current = current.next
}
return string
}
this.print = function() {}
}
var linkedList = new LinkedList();
linkedList.append("shidengyun");
linkedList.insert(0, 'zhujing');
console.log(linkedList.toString());

JavaScript LinkedList的更多相关文章

  1. javascript LinkedList js 双向循环链表 Circular Linked List

    javascript LinkedList: function Node(elem, prev, next) { this.elem = elem; this.prev = prev ? prev : ...

  2. javascript linkedlist data structures

    在使用C++的时候我们经常会使用到各种容器,这些容器其实就是一种数据结构.在java中其实也是如此.但是由于javascript只给我们提供了一种内置的数据结构数组,准备来说是对象.没有我们常见的那些 ...

  3. 用JavaScript来实现链表LinkedList

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文地址. 写在前面 好多做web开发的朋友,在学习数据结构和算法时可能比较讨厌C和C++,上学的时候写过的也忘得差不多了,更别提没写过的了.但幸运 ...

  4. 【JavaScript数据结构系列】05-链表LinkedList

    [JavaScript数据结构系列]05-链表LinkedList 码路工人 CoderMonkey 转载请注明作者与出处 ## 1. 认识链表结构(单向链表) 链表也是线性结构, 节点相连构成链表 ...

  5. 学习javascript数据结构(二)——链表

    前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...

  6. 数据结构与算法之链表-javascript实现

    链表的定义: 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点 ...

  7. javascript数据结构和算法

    一.栈 javascript实现栈的数据结构(借助javascript数组原生的方法即可) //使用javascript来实现栈的数据结构 var Stack={ //不需要外界传参进行初始化,完全可 ...

  8. javascript中的链表结构

    1.定义 很多编程语言中数组的长度是固定的,就是定义数组的时候需要定义数组的长度,所以当数组已经被数据填满的时候,需要再加入新的元素就很困难.只能说在部分变成语言中会有这种情况,在javascript ...

  9. 《数据结构与算法JavaScript描述》

    <数据结构与算法JavaScript描述> 基本信息 作者: (美)Michael McMillan 译者: 王群锋 杜欢 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ISBN:9 ...

随机推荐

  1. JS同步执行代码

    new Promise(function(){initAppToken()}).then(()=>                     getApps(this.pageInfo).then ...

  2. Nginx优化_自定义报错页面

    自定义返回给客户端的404错误页面 1. 优化前,客户端使用浏览器访问不存在的页面,会提示404文件未找到 client]# firefox http://192.168.4.5/xxxxx      ...

  3. python面向对象--类的内置函数

    #isinstance(obj,cls)判断obj是否是类cls的实例 #issubclass(cls,cls1)判断cls是否是cls1的子类或派生类 class Foo: pass class B ...

  4. uniq 去除重复行

    1.命令功能 uniq可以输出或忽略文件中的重复行,经常需要使用sort先对文件进行排序,然后使用uniq去重并计数. 2.语法格式 uniq  option  input uniq   选项    ...

  5. wireshare文件格式

    你用Wireshark打开这个pkt试试,如果可以打开,就说明Wireshark支持这种格式.然后你就可以去看Wireshark的源码.*.pkt是omnipeek/etherpeek的默认文件格式, ...

  6. bzoj3252 攻略 贪心+dfs序+线段树

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3252 题解 有一个非常显然的贪心思路:每次选择目前走到那儿能够获得的新权值最大的点. 证明的话 ...

  7. CF1090J Two Prefixes

    神仙题++ 还是在某校梁大讲的题qaq 我们考虑容斥 也就是本质不同字串=全部-重复的 我们只需要求重复的即可 考虑相同的s=ab 我们用长度最长的a作为代表串 如果存在一个a'b'且|a'|> ...

  8. rest认证组件,权限组件,频率组件,url注册器,响应器组件,分页器组件

    1.认证组件 1.1 认证组件利用token来实现认证 1.2 token认证的大概流程 用户登录===>获取用户名和密码===>查询用户表 如果用户存在,生成token,否则返回错误信息 ...

  9. TFRecords文件的生成和读取(1)

    参考:https://blog.csdn.net/u012222949/article/details/72875281 参考:https://blog.csdn.net/chengshuhao199 ...

  10. vue项目-本机ip地址访问

    修改 在 vue项目文件夹中的 package.json scripts >dev 添加 --host 0.0.0.0 "dev": "webpack-dev-se ...