javaScript数据结构--双向链表

双向链表和普通链表的区别是,普通链表中一个节点只有一个next指针指向下一个节点,双向链表有2个指针,一个指向下一个节点,一个指向前面一个节点。

双向链表的结构图

双向链表的代码实现:

function DoubleLinkedList() {

    var Node = function(element) {

        this.element = element;

        this.next = null;

        this.prev = null;

    }

    var head = null,

        tail = null,

        length = 0;

    this.append = function(element) {

        const node = new Node(element);

        if(length === 0) {

            head = node;

            tail = node;

        }else {

            tail.next = node;

            node.prev = tail;

            tail = node;

        }

        length++;

        return true;

    }


    this.insert = function(position, element) {

        if(position>=0 && position <=length) {

            var current = head,

                previous,

                index = 0;

            const node = new Node(element);

            if(position === 0) {

                if(!head) {

                    head = node;

                    tail = node;

                } else {

                    node.next = current;

                    current.prev = node;

                    head = current;

                }

            } else if(position === length) {

                current = tail;

                node.prev = current;

                current.next = node;

                tail = node;

            }else {

                while(index++ < position) {

                    previous = current;

                    current = current.next;

                }

                node.prev = previous;

                previous.next = node;

                node.next = current;

                current.prev = node;

            }

            length++;

            return true;

        }else {

            return false;

        }

    }

    this.removeAt = function(position) {

        if(position>=0 && position < length) {

            var current = head,

                previous,

                index = 0;

            if(position === 0) {

                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;

        }

    }

    this.remove = function(element) {

        const position = this.indexOf(element);

        return this.removeAt(position);

    }

    this.indexOf = function(element) {

        var current = head,

            previous,

            index = 0;

        while(current) {

            if(element === current.element) {

                return index;

            }

            index++;

            current = current.next;

        }

    }

    this.isEmpty = function() {

        return length === 0

    }

    this.size = function() {

        return this.length

    }

    this.getHead = function() {

        return head;

    }

    this.getTail = function() {

        return tail;

    }

    this.toString = function() {

        var current = head,

        string = '';

        while(current) {

            string += String(current.element);

            current = current.next;

        }

        return string;

    }

}

var list = new DoubleLinkedList();

list.append('aa');

list.append('bb');

console.log(list.toString()); // aabb

list.insert(1, 'cc')

list.insert(1, 'dd')

console.log(list.toString()); // aaddccbb

list.removeAt(1);

console.log(list.toString()); // aaccbb

console.log(list.indexOf('cc')); // 1

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 如需转载, 请咨询作者, 并且注明出处.有任何问题, 可以关注我的微博: coderwhy, 或者添加我的微信: ...
    coderwhy阅读 7,955评论 1 30
  • 一. 认识双向链表 双向链表介绍 单向链表: 我们可以轻松的到达下一个节点, 但是回到钱一个节点是很难的. 但是,...
    小码哥教育520it阅读 15,255评论 0 0
  • 1数组 1.1方法列表 数组的常用方法如下: concat: 链接两个或者更多数据,并返回结果。 every: 对...
    奋斗1216阅读 304评论 0 1
  • 关于石头爱情传奇(游大连 一岛) <一> 那个静坐千年的石头 终日面对大海 苦思冥想 面对我的第一次到来 它的...
    云水梦阅读 359评论 0 1
  • 没有宙斯的神话没有星光的银河没有太阳的宇宙只剩下你,只剩下我…… 我不想带你去看肃杀的景致怕灼伤你无辜的双眼请你,...
    Minnie沫阅读 128评论 0 0