javascript双向链表

        // 双向链表
        function DoublyLinkedList() {
            function Node(data) {
                this.data = data;
                this.prev = null;
                this.next = null;
            }

            // 当前头部
            this.head = null;
            // 当前尾部
            this.tail = null;
            this.length = 0;

            DoublyLinkedList.prototype.append = function (data) {
                // 创建节点
                var newNode = new Node(data);
                if (this.length == 0) {
                    // 只有一个节点时头部和尾部指向位置相同
                    this.head = newNode;
                    this.tail = newNode;
                } else {
                    // 当前尾部位置移动为新节点的上一位置
                    newNode.prev = this.tail;
                    this.tail.next = newNode;
                    this.tail = newNode;  // 当前尾部指向新节点
                }
                this.length += 1;
            }

            // 从前往后读取
            DoublyLinkedList.prototype.backwordString = function () {
                var current = this.head; // 指向第一个
                var resultString = "";
                // 当前节点不是空的话往后移动,直到节点为空停止循环
                while (current) {
                    resultString += current.data + " ";
                    current = current.next;
                }
                return resultString;
            }
            // 从后往前读取
            DoublyLinkedList.prototype.forwardString = function () {
                var current = this.tail; // 指向最后一个
                var resultString = "";
                while (current) {
                    resultString += current.data + " ";
                    current = current.prev;
                }
                return resultString;
            }
            DoublyLinkedList.prototype.toString = function () {
                return this.backwordString();
            }

            DoublyLinkedList.prototype.insert = function (position, data) {
                if (this.position < 0 || position > this.length) return false;
                var newNode = new Node(data);
                // 没有任何节点
                if (this.length == 0) {
                    this.head = newNode;
                    this.tail = newNode;
                } else {
                    // 判断当前节点是否为0
                    if (position == 0) {
                        // 原来第一个节点指向新的node
                        this.head.prev = newNode;
                        // 新节点指向原来的第一个节点
                        newNode.next = this.head;
                        this.head = newNode;
                    } else if (position == this.length) {
                        // 当前尾部位置移动为新节点的上一位置
                        newNode.prev = this.tail;
                        this.tail.next = newNode;
                        this.tail = newNode;  // 当前尾部指向新节点
                    } else {
                        // 插入到中间某个位置
                        var current = this.head;
                        var index = 0;
                        while (index++ < position) {
                            current = current.next;
                        }
                        newNode.next = current;
                        newNode.prev = current.prev;
                        // 先将新节点赋值给前一节点的下一节点
                        current.prev.next = newNode;
                        // 再讲当前节点的前一节点指向新节点
                        current.prev = newNode;
                    }
                }
                this.length += 1;
            }

            DoublyLinkedList.prototype.get = function (position) {
                if (this.position < 0 || position >= this.length) return null;
                // 获取元素
                var current = this.head;
                var index = 0;
                while (index++ < position) {
                    current = current.next;
                }
                return current.data;
            }

            DoublyLinkedList.prototype.indexOf = function (data) {
                // 获取元素
                var current = this.head;
                var index = 0;
                while (current) {
                    if (current.data == data) {
                        return index;
                    }
                    current = current.next;
                    index += 1;
                }
                return -1;

            }

            DoublyLinkedList.prototype.update = function (position, newdata) {
                if (this.position < 0 || position >= this.length) return false;
                // 获取元素
                var current = this.head;
                var index = 0;
                while (index++ < position) {
                    current = current.next;
                }
                current.data = newdata;
                return true;
            }

            DoublyLinkedList.prototype.removeAt = function (position) {
                if (this.position < 0 || position > this.length) return null;
                var current = this.head;
                if (this.length == 1) {
                    this.head = null;
                    this.tail = null;
                } else {
                    if (position == 0) {
                        this.head.next.prev = null;
                        this.head = this.head.next;
                    }
                    else if (position == this.length - 1) {
                        // 删除的是最后一个节点
                        current = this.tail;
                        this.tail.prev.next = null;
                        this.tail = this.tail.prev;
                    }
                    else {
                        var index = 0;
                        while (index++ < position) {
                            current = current.next;
                        }
                        // 当前节点的前一个节点指向当前节点的下一个节点
                        current.prev.next = current.next;
                        current.next.prev = current.prev;
                    }
                }
                this.length -= 1;
                return current.data;
            }

            
            DoublyLinkedList.prototype.remove = function (data) {
                var index = this.indexOf(data);
                return this.removeAt(index);
            }

            DoublyLinkedList.prototype.isEmpty = function () {
                return this.length == 0;
            }
            
            DoublyLinkedList.prototype.Size = function () {
                return this.length;
            }

            DoublyLinkedList.prototype.getHead = function () {
                return this.head.data;
            }
            DoublyLinkedList.prototype.getTail = function () {
                return this.tail.data;
            }
        }

        var linkedlist = new DoublyLinkedList();
        linkedlist.append("abc");
        linkedlist.append("bcd");
        linkedlist.append("cde");
        // linkedlist.insert(0,"000");
        // linkedlist.insert(4,"444");
        // linkedlist.insert(2,"222");
        // linkedlist.update(2,"222");
        // alert(linkedlist.get(3));
        // alert(linkedlist.backwordString());
        // alert(linkedlist.forwardString());
        // alert(linkedlist.indexOf("444"));
        //linkedlist.removeAt(1);
       // linkedlist.remove("bcd");
       // alert(linkedlist.toString());
       alert(linkedlist.getHead());
       alert(linkedlist.getTail());

 

posted @ 2020-06-13 20:32  bradleydan  阅读(76)  评论(0)    收藏  举报