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. overflow的量两种模式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Linux查看当前目录下所有文件中包含map的行记录

    find yaochi_e.prm |xargs grep -ri "map" grep -n "map" *.prm|grep -v "\-\-ma ...

  3. JavaScript——call() 方法

    function Product(name, price) { this.name = name; this.price = price; } function Food(name, price) { ...

  4. ubuntu idea 安装

    一.下载 1.进入官网 下载对应安装包 https://www.jetbrains.com/idea/download/#section=linux sudo wget https://downloa ...

  5. canvas一个简单粗暴的中奖转盘

    最近在学canvas做动画,于是就写个转盘练下手.上个简陋的成果图(中间那个是转的指针,外面的圈是图片,懒得写了哈哈哈) 代码很简单,都注释了,直接上代码吧,嘤嘤嘤 html <body> ...

  6. SNOI2017 礼物

    题解 设前\(n\)个人的礼物个数和为\(F_n\), 那么显然\[F_n = 2 \times F_{n-1} + i^k\] 考虑矩阵快速幂 棘手的问题是:\(i^k\)不是可以直接用矩阵乘法可以 ...

  7. orm中 如何模糊匹配某一年的用户和某一事时间段的用户

    导入Q查询

  8. Listary安装+破解

    Listary 是 windows 下一个可以快速搜索所有程序,文件,并且可以快速启动程序和打开相应文件的这么一个工具,但这只是它的一个主要功能,还有其功能也是很屌的. 详细的介绍参考:[Listar ...

  9. Leetcode_1278. Palindrome Partitioning III_[DP]

    题目链接 You are given a string s containing lowercase letters and an integer k. You need to : First, ch ...

  10. springmvc上传文件异常

    症状: error:org.springframework.web.multipart.MultipartException: Current request is not a multipart r ...