链表

作者: _____西班木有蛀牙 | 来源:发表于2018-06-29 23:51 被阅读13次
  • 数组不是组织数据的最佳结构
  • 在做删除和插入操作的时候我们需要将其它的元素前移/后移,这样执行效率会过低
  • javascript 的数组被实现成了对象,与其他语言数组相比,效率低了很多
  • 除了对数据的随机访问,链表几乎可以用在任何可以使用以为数组的地方

单向链表


// singleLinkedList
function Node (element) {
    this.element = element;
    this.next = null;
}
function LList () {
    this.head = new Node('head');
    this.find = find;
    this.insert = insert;
    this.display = display;
    this.findPrevious = findPrevious;
    this.remove = remove;
}

function find (item) {
    // 查找
    var currNode = this.head;
    while (currNode.element !== item) {
        currNode =currNode.next;
    }
    return currNode;
}
function insert (newElement, item) {
    // 插入
    var newNode= new Node(newElement);
    var currNode = this.find(item);
    newNode.next = currNode.next;
    currNode.next = newNode;
}
function display (){
    // 遍历
    var currNode = this.head;
    while(currNode.next !== null) {
        console.log(currNode.next.element);
        currNode = currNode.next;
    }
}
function findPrevious (item) {
    var currNode = this.head;
    while (currNode.next !== null && currNode.next.element !== item) {
        currNode = currNode.next;
    }
    return currNode;
}

function remove (item) {
    var preNode = this.findPrevious(item);
    var currNode = this.find(item);
    if (preNode.next !== null) {
        preNode.next = currNode.next;
        currNode.next = null
    }
}

var cities = new LList();
cities.insert('first', 'head');
cities.insert('second', 'first');
cities.insert('thrid', 'second');
cities.display();
console.log('===================')
cities.remove('second');
cities.display();

双向链表


// doubleLinkedList
function Node (element) {
    this.element = element;
    // 后
    this.next = null;
    // 前
    this.previous = null;
}
function LList () {
    this.head = new Node('head');
    this.find = find;
    this.insert = insert;
    this.display = display;
    this.remove = remove;
    this.displayReverse = displayReverse;
    this.findLast = findLast;
}
// 查找
function find (item) {
    var currNode = this.head;
    while (currNode.element !== item) {
        currNode = currNode.next
    }
    return currNode;
}
// 插入
function insert (newElement, item) {
    var newNode = new Node(newElement);
    var currNode = this.find(item);
    newNode.next = currNode.next;
    newNode.previous = currNode;
    currNode.next = newNode;
    if (!(newNode.next === null)) {
        newNode.next.previous = newNode;
    }
}
// 遍历
function display () {
    var currNode = this.head;
    console.log('遍历')
    while (currNode.next !== null) {
        console.log(currNode.next.element);
        currNode = currNode.next;
    }
    console.log('== end ==')
}
// 删除
function remove (item) {
    var currNode = this.find(item);
    if (!(currNode.next == null)) {
        currNode.previous.next = currNode.next;
        currNode.next.previous = currNode.previous;
        currNode.next = null;
        currNode.previous = null;
    } else {
        // 尾节点
        currNode.previous.next = null;
        currNode.previous = null;
    }
}
// 查找最后一个节点
function findLast () {
    var currNode = this.head;
    while(!(currNode.next == null)) {
        currNode = currNode.next
    }
    return currNode
}
// 反序
function displayReverse () {
    var currNode = this.findLast()
    console.log('反序:')
    while(!(currNode.previous == null)) {
        console.log(currNode.element);
        currNode = currNode.previous;
    }
    console.log('== end ==')
}

var cities = new LList();
cities.insert('first', 'head');
cities.insert('second', 'first');
cities.insert('thrid', 'second');
cities.display();
cities.remove('second');
cities.display();
cities.displayReverse();

相关文章

  • 链表基础

    链表基础 链表长度 链表为空 链表结构 链表增加

  • 双向链表&双向循环链表

    链表分为:单链表、单向循环链表、双向链表、双向循环链表本节主要说明:双向链表、双向循环链表 定义结点 一、双向链表...

  • 算法与数据结构:链表

    链表 链表还分为单向链表和双向链表, 但是这篇文章只说单向链表 , 下次再讲双向链表 . 链表和数组的区别 ? 链...

  • 链表

    链表 单链表反转链表中环的检测两个有序链表合并删除链表倒数第n个节点求链表的元素总个数 一.单向链表 链表共有特征...

  • 五、双向链表

    双向链表 此前介绍的链表,也叫做单向链表使用双向链表可以提升链表的综合性能 修改之前的单链表的源码: 双向链表 –...

  • 链表

    内容 链表数据结构 向链表添加元素 从链表移除元素 使用 LinkedList 表 双向链表 循环链表 链表数据结...

  • 数据与算法结构

    线性表 顺序表 链表(物理上离散,逻辑上连续) 链表的类别 单链表 循环链表 双链表 链表的操作 顺序表与链表的比...

  • 数据结构——链表

    本文所讲的链表是单链表,链表采用无头链表 科普下:一般链表可以分为有头节点的链表与无头节点的链表 有头节点的链表:...

  • 链表

    文章结构 链表的定义 链表的插入和删除操作 链表的特性 常见的链表结构 自定义链表 链表的经典操作 使用链表实现L...

  • Algorithm小白入门 -- 单链表

    单链表递归反转链表k个一组反转链表回文链表 1. 递归反转链表 单链表节点的结构如下: 1.1 递归反转整个单链表...

网友评论

      本文标题:链表

      本文链接:https://www.haomeiwen.com/subject/tcloyftx.html