写代码的真是心细啊,每一步操作的先后顺序都在卡准。

我其实只是理解了思想和大概的操作。

真正要用时,可能还是要复制,粘贴。。。:)

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.toString = function(){
        var current = head,
            string = '';
        while (current){
            string += current.element;
            current = current.next;
        }
        return string;
    };

    this.getHead = function(){
        return head;
    }

    this.print = function(){

    };
}

var list = new LinkedList();
list.append(15);
list.append(10);
console.log(list.toString());

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.insert = function(position, element){
        if(position >=0 && position <= length){
            var node = new Node(element),
                current = head,
                previous,
                index = 0;

            if (position === 0){
                if (!head){
                    head = node;
                    tail = node;
                } else {
                    node.next = current;
                    current.prev = node;
                    head = node;
                }
            } else if (position === length){
                current = tail;
                current.next = node;
                node.prev = current;
                tail = node;
            } else {
                while (index++ < position){
                    previous = current;
                    current = current.next;
                }
                node.next = current;
                previous.next = node;

                current.prev = node;
                node.prev = previous;
            }
            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;
                if (length === 1){
                    tail = null;
                } else {
                    head.prev = null;
                }
            } else if(position == length-1){
                current = tail;
                tail = current.prev;
                tail.next = null;
            } else {
                while (index++ < position){
                    previous = current;
                    current = current.next;
                }
                previous.next = current.next;
                current.next.prev = previous;
            }
            length--;
            return current.element;
        } else {
            return null;
        }
    }
}

javascript普通链表及双向链表的更多相关文章

  1. 数据结构与算法JavaScript (三) 链表

    我们可以看到在javascript概念中的队列与栈都是一种特殊的线性表的结构,也是一种比较简单的基于数组的顺序存储结构.由于javascript的解释器针对数组都做了直接的优化,不会存在在很多编程语言 ...

  2. 线性链表的双向链表——java实现

    .线性表链式存储结构:将采用一组地址的任意的存储单元存放线性表中的数据元素. 链表又可分为: 单链表:每个节点只保留一个引用,该引用指向当前节点的下一个节点,没有引用指向头结点,尾节点的next引用为 ...

  3. Python 单向链表、双向链表

    用面向对象实现Linkedlist链表 单向链表实现append.iternodes 双向链表实现append.pop.insert.remove.iternodes 单向链表与双向链表 单向链表: ...

  4. 【 C# 】(一) ------------- 泛型带头节点的单链表,双向链表实现

    在编程领域,数据结构与算法向来都是提升编程能力的重点.而一般常见的数据结构是链表,栈,队列,树等.事实上C#也已经封装好了这些数据结构,在头文件 System.Collections.Generic ...

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

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

  6. JavaScript数据结构——链表

    链表:存储有序的元素集合,但不同于数组,链表中的元素在内存中不是连续放置的.每个元素由一个存储元素本身的节点和一个指向下一个元素的引用(也称指针或链接)组成. 好处:可以添加或移除任意项,它会按需扩容 ...

  7. JavaScript数据结构——链表的实现

    前面楼主分别讨论了数据结构栈与队列的实现,当时所用的数据结构都是用的数组来进行实现,但是数组有的时候并不是最佳的数据结构,比如在数组中新增删除元素的时候需要将其他元素进行移动,而在javascript ...

  8. JavaScript数据结构——链表的实现与应用

    链表用来存储有序的元素集合,与数组不同,链表中的元素并非保存在连续的存储空间内,每个元素由一个存储元素本身的节点和一个指向下一个元素的指针构成.当要移动或删除元素时,只需要修改相应元素上的指针就可以了 ...

  9. Python链表的实现与使用(单向链表与双向链表)

    参考[易百教程]用Python实现链表及其功能 """ python链表的基本操作:节点.链表.增删改查 """ import sys cl ...

随机推荐

  1. App接口简介

  2. 如果你也和我一样,OSX反应慢,不妨试试这个

  3. web漏洞总结

    目录: 1.sql注入获取数据库信息2.sql注入绕过管理后台登录3.反射型xss4.存储型xss5.csrf6.文件上传7.暴力破解8.目录遍历9.权限跨越10.文件包含11.未知漏洞 web漏洞演 ...

  4. SilverLight 条码 扫描枪 MVVM(转载)

    目前做SL项目时,遇到条码的问题. 客户需要通过条码来加快工作效率以及减少错误.有条码,自然便引入扫描枪这个东西.不解释. 关键点是:扫描枪每次扫描完毕会触发回车 这是解决问题的关键! 现有两种情景: ...

  5. Ubuntu 中软件的安装、卸载以及查看的方法总结

    Ubuntu 中软件的安装.卸载以及查看的方法总结 博客分类: Linux UbuntuDebian配置管理CacheF#  说明:由于图形化界面方法(如Add/Remove... 和Synaptic ...

  6. 修改shell 将当前shell(默认是bash B SHELL )改为csh C SHELL

     在修改当前shell时,用命令: usermod -s  /bin/csh   home     home 为 你所想要改变的用户地址     此处home 为家目录,一般自己创建的用户都会在家目录 ...

  7. mysql 查询技巧

    查出来的结果每一行显示一条,中间以*号分隔. select * from tableName limit 10 \G mysql 随机取数据 SELECT * FROM table_name ORDE ...

  8. 【整理】Angularjs 监听ng-repeat onfinishrender事件

    http://stackoverflow.com/questions/15207788/calling-a-function-when-ng-repeat-has-finished http://ww ...

  9. cf.295.C.DNA Alignment(数学推导)

    DNA Alignment time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  10. [Effective JavaScript 笔记]第47条:绝不要在Object.prototype中增加可枚举的属性

    之前的几条都不断地重复着for...in循环,它便利好用,但又容易被原型污染.for...in循环最常见的用法是枚举字典中的元素.这里就是从侧面提出不要在共享的Object.prototype中增加可 ...