javascript实现双向链表数据结构

function DoublyLinkedList() {
    let Node = function (element) {
        this.element = element;
        this.next = null;//下一个链
        this.prev = null;//上一个链
    };

    let length = 0;
    let head = null;//链头
    let tail = null;//链尾
    /**
     * 向双向链表添加一个元素
     * @param element 元素值
     */
    this.append = function (element) {
        //创建一个双向链表节点
        let node = new Node(element),
            current;
        if (head === null) {
            head = node;
            tail = node;
        } else {
            current = head;
            while (current.next) {
                current = current.next;
            }
            current.next = node;
            node.prev = current;
            tail = node;
        }
        length++;

    };

    /**
     * 向双向链表插入一个元素
     * @param position 插入位置
     * @param element  插入元素值
     * @returns {boolean}
     */
    this.insert = function (position, element) {
        //检查越界值
        if (position >= 0 && position <= length) {
            let 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;
        }
    };

    /**
     *  以字符串方式返回双向链表
     * @param num 1从链尾排序返回 0从链头排序返回
     * @returns {string}
     */
    this.toString = function (num) {
        if (num == 1) {
            let current = tail, string = '';
            while (current) {
                string += current.element + (current.prev ? ',' : '');
                current = current.prev;
            }
            return string;
        } else if (num == 0) {
            let current = head, string = '';
            while (current) {
                string += current.element + (current.next ? ',' : '');
                current = current.next;
            }
            return string;
        }

    };

    /**
     * 根据链表索引值删除节点
     * @param position 索引值
     * @returns {*}
     */
    this.removeAt = function (position) {
        if (position > -1 && position < length) {
            let 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;
        }
    };

    /**
     * 根据节点值删除一个节点
     * @param element
     * @returns {*}
     */
    this.remove = function (element) {
        let index = this.indexOf(element);
        return this.removeAt(index);
    };

    //根据节点值获取索引
    this.indexOf = function (element) {
        let current = head,
            index = 0;
        while (current) {
            if (element === current.element) {
                return index;
            }
            index++;
            current = current.next;
        }
        return -1;
    };


    /***
     * 根据链表索引获取节点值
     * @param index  索引
     * @returns {*}
     */
    this.getNode = function (index) {
        // 判断参数有效性
        if (index < 0 || index >= this.size()) {
            console.log("获取节点失败!索引出界了!");
            return null;
        }
        let rs;//保存返回结果
        if (index === 0) {  //链头
            rs = head;
            return rs.element;
        }

        if (index === length - 1) { //链尾
            rs = tail;
            return rs.element;
        }


        // 正向查找
        if (index <= this.size() / 2) {
            let i = 0;
            findex = head.next;
            while (i++ < index) {
                rs = findex.element;
                findex = findex.next;
            }
            return rs;
        }

        // 反向查找
        let j = 0;
        let rindex = this.size() - index - 1;
        prindex = tail.prev;
        while (j++ < rindex) {
            rs = prindex.element;
            prindex = prindex.prev;
        }
        return rs;
    };

    //判断双向链表是否为空
    this.isEmpty = function () {
        return length === 0;
    };

    //获取双向链表长度
    this.size = function () {
        return length;
    };

    //获取双向链表头
    this.getHead = function () {
        return head;
    };

    //获取双向链表尾
    this.getfoot = function () {
        return tail;
    }
}

//let list = new DoublyLinkedList();
//list.append(1234);
//list.append(5123);
//list.append(4512);
//list.append(3451);
//
//
//let list1 = new DoublyLinkedList();
//list1.append(-1111);
//list1.append(1111);
//list1.append(1111);
//list1.append(1111);
//
//
//
循环打印节点
//var astr='';
//for (var i = 0; i < list.size(); i++) {
//    astr+=list.getNode(i);
//}
//
循环打印节点
//var bstr='';
//for (var j = 0; j < list1.size(); j++) {
//    bstr+=list1.getNode(j);
//}
//
//console.log(astr);
//console.log(bstr);
//var a=parseInt(astr);
//var b=parseInt(bstr);
//
//console.log(a+b);

var alink=new DoublyLinkedList();
var testnum='-9999';
alink.append(testnum);
alink.append('9999');

var blink=new DoublyLinkedList();

blink.append('1');
blink.append('0000');
blink.append('0000');
blink.append('0000');



//循环打印节点
var astr='';
for (var i = 0; i < alink.size(); i++) {
    astr+=alink.getNode(i);
}

//循环打印节点
var bstr='';
for (var j = 0; j < blink.size(); j++) {
    bstr+=blink.getNode(j);
}


var a=parseInt(astr);
var b=parseInt(bstr);

var rsa=astr+"+"+bstr+"="+(a+b);
var rsb=astr+"-"+bstr+"="+(a-b);

console.log(rsa);
console.log(rsb);
//结果
//-99999999+1000000000000=999900000001
//-99999999-1000000000000=-1000099999999

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值