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(pos,element){
if (pos>=0 && pos<=length) {
var node=new Node(element),current=head,previous,index=0;
if(position===0){
node.next=current;
head=node;
}else{
while (index++<pos) {
previous=current;
current=current.next;
}
node.next=current;
previous.next=node;
}
length++;
return true;
}else{
return false;
}
};
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;
}
previous.next=current.next;
}
length--;
return current.element;
}else{
return null;
}
};
this.remove=function(element){
var index=this.indexOf(element);
return this.removeAt(index);//不太好,重复遍历了2次
};
this.indexOf=function (element) {
var current=head,index=0;
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.toString=function(){
var current=head,str='';
while (current) {
str+=","+current.element;
current=current.next;
}
return str.slice(1);
};
this.print=function(){
console.log(this.toString());
};
this.getHead=function(){
return head;
}
}
function DoublyLinkedList() {
// 辅助类,表示加入链表的每一项
var Node=function(element){
this.element=element;
this.next=null;
this.prev=null;
}
// 列表,存储链表的长度
var length=0;
var head=null;
var tail=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(pos,element){
if (pos>=0 && pos<=length) {
var node=new Node(element),current=head,previous,index=0;
if(pos===0){
if(!head){
head=node;
tail=node;
}else{
node.next=current;
current.prev=node;
head=node;
}
}else if(pos==length){
current=tail;
current.next=node;
node.prev=current;
tail=node;
}else{
while (index++<pos) {
previous=current;
current=current.next;
}
node.next=current;
node.prev=previous;
previous.next=node;
current.prev=node;
}
length++;
return true;
}else{
return false;
}
};
this.removeAt=function(pos){
if(pos>-1 && pos<length){
var current=head,previous,index=0;
if(pos==0){
head=current.next;
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;
}
previous.next=current.next;
current.next.prev=current.prev;
}
length--;
return current.element;
}else{
return null;
}
};
this.remove=function(element){
var index=this.indexOf(element);
return this.removeAt(index);//不太好,重复遍历了2次
};
this.indexOf=function (element) {
var current=head,index=0;
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.toString=function(){
var current=head,str='';
while (current) {
str+=","+current.element;
current=current.next;
}
return str.slice(1);
};
this.print=function(){
console.log(this.toString());
};
this.getHead=function(){
return head;
}
}

  

Javascript中的链表的更多相关文章

  1. javascript中的链表结构—从链表中删除元素

    1.概念 上一个博文我们讲到链表,其中有一个方法remove()是暂时注释的,这个方法有点复杂,需要添加一个Previous()方法找到要删除的元素的前一个节点,这一个博文我们来分析一下这个remov ...

  2. javascript中的链表结构

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

  3. javascript中在链表中向前(向后)移动n个节点

     1.概念 在链表上移动n个节点,我第一眼看到这个需求的时候首先想到的是当前节点.使用这个当前节点作为参考来移动,没有这个当前节点的话是没有办法在链表上前进和后退的.初始化定义链表的时候定义一个当前节 ...

  4. javascript中的链表结构—双向链表

    1.概念 上一个文章里我们已经了解到链表结构,链表的特点是长度不固定,不用担心插入新元素的时候新增位置的问题.插入一个元素的时候,只要找到插入点就可以了,不需要整体移动整个结构. 这里我们了解一下双向 ...

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

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

  6. JavaScript中的算法之美——栈、队列、表

    序 最近花了比较多的时间来学习前端的知识,在这个期间也看到了很多的优秀的文章,其中Aaron可能在这个算法方面算是我的启蒙,在此衷心感谢Aaron的付出和奉献,同时自己也会坚定的走前人这种无私奉献的分 ...

  7. JavaScript 中 4 种常见的内存泄露陷阱

    了解 JavaScript 的内存泄露和解决方式! 在这篇文章中我们将要探索客户端 JavaScript 代码中常见的一些内存泄漏的情况,并且学习如何使用 Chrome 的开发工具来发现他们.读一读吧 ...

  8. JavaScript中的内存泄漏以及如何处理

    随着现在的编程语言功能越来越成熟.复杂,内存管理也容易被大家忽略.本文将会讨论JavaScript中的内存泄漏以及如何处理,方便大家在使用JavaScript编码时,更好的应对内存泄漏带来的问题. 概 ...

  9. JavaScript 中常见的内存泄露陷阱(摘)

    内存泄露是每个开发者最终都不得不面对的问题.即便使用自动内存管理的语言,你还是会碰到一些内存泄漏的情况.内存泄露会导致一系列问题,比如:运行缓慢,崩溃,高延迟,甚至一些与其他应用相关的问题. 什么是内 ...

随机推荐

  1. PAT复杂度_最大子列和问题、最大子列和变种

    01-复杂度1. 最大子列和问题 给定K个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j ...

  2. adminLTE的自动化菜单

    一. model from django.db import models # Create your models here. class MenuInfo(models.Model): paren ...

  3. JavaSE基础第一篇

      1.JDK的安装: 包括JRE 和JVM   下载地址: www.oracle.com/www.sun.com 2.环境变量 set path = "bin所在路径" 设置pa ...

  4. iOS常用设计模式笔记

    一.原则 1.单一职责原则 (Single Responsiblity Principle SRP) 2.开闭原则(Open Closed Principle,OCP) 3.里氏替换原则(Liskov ...

  5. 增加VirtualBox虚拟机的磁盘空间大小(Host:Win7 VirtualBox5.0.16 VM:Win10)

    1 前言 网上关于增加VirtualBox虚拟机的磁盘空间大小的文章非常非常多,这里我之所以再写一篇,是因为在参照这些文章做的时候,由于VirtualBox的版本更新以及其他一些环境问题,碰到到一些问 ...

  6. java加法流程图

  7. python之编写登陆接口(第一天)

    作业:编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 针对此实例写了有二种类型的脚本,略有不同,具体如下: 帐号文件account.txt内容如下: sam 123 david ...

  8. VB 中 NumericUpDown 控件 如何为手动输入设定触发事件

    Private Sub numDuration_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) ...

  9. ClientScript.RegisterStartupScript 不起作用

    asp.net webform 使用 ClientScript.RegisterStartupScript 不起作用 form 加上 runat="server",ok

  10. 基于CNN的人脸相似度检测

    人脸相似度检测主要是检测两张图片中人脸的相似度,从而判断这两张图片的对象是不是一个人. 在上一篇文章中,使用CNN提取人脸特征,然后利用提取的特征进行分类.而在人脸相似度检测的工作中,我们也可以利用卷 ...