【算法】双链表

该文章展示了一个JavaScript类,用于创建和操作双链表。类包含方法如添加元素、在指定位置插入元素、删除元素、向前和向后遍历链表、获取元素、检查元素存在性以及获取链表的长度等。
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            class DoubleLinkedList {
                head = null;
                tail = null;
                length = 0;
                static Node = class Node {
                    constructor(key) {
                        this.key = key;
                        this.next = null;
                        this.prev = null;
                    }
                };
                append(key) {
                    let newNode = new DoubleLinkedList.Node(key);
                    if (this.length === 0) {
                        this.head = newNode;
                        this.tail = newNode;
                    } else {
                        this.tail.next = newNode;
                        newNode.prev = this.tail;
                        this.tail = newNode;
                    }
                    this.length += 1;
                    return newNode.key;
                }
                insert(position, key) {
                    if (position < 0 || position > this.length) {
                        return -1;
                    }

                    let newNode = new DoubleLinkedList.Node(key);
                    if (this.length === 0) {
                        this.head = newNode;
                        this.tail = newNode;
                    } else {
                        if (position === 0) {
                            this.head.prev = newNode;
                            newNode.next = this.head;
                            this.head = newNode;
                        } else if (position === this.length - 1) {
                            this.tail.next = newNode;
                            newNode.prev = this.tail;
                            this.tail = newNode;
                        } else {
                            let len = Math.floor(this.length / 2);
                            if (len > position) {
                                let current = this.head;
                                let index = 0;
                                while (index++ < position) {
                                    current = current.next;
                                }
                                newNode.prev = current.prev;
                                newNode.next = current;
                                current.prev.next = newNode;
                                current.prev = newNode;
                            } else {
                                let current = this.tail;
                                let index = this.length - 1;
                                while (index-- > position) {
                                    current = current.prev;
                                }
                                newNode.prev = current.prev;
                                newNode.next = current;
                                current.prev.next = newNode;
                                current.prev = newNode;
                            }
                        }
                    }

                    this.length += 1;
                    return newNode.key;
                }
                forwardString() {
                    let current = this.head;
                    let str = "";
                    while (current) {
                        str += current.key + "=>";
                        current = current.next;
                    }
                    return str;
                }
                backwardString() {
                    let current = this.tail;
                    let str = "";
                    while (current) {
                        str += current.key + "=>";
                        current = current.prev;
                    }
                    return str;
                }
                indexOf(key) {
                    let current = this.head;
                    let index = 0;
                    while (current) {
                        if (current.key === key) {
                            return index;
                        }
                        index += 1;
                        current = current.next;
                    }
                    return -1;
                }
                get(position) {
                    if (position < 0 || position > this.length - 1) {
                        return -1;
                    }
                    if (position === 0) {
                        return this.head.key;
                    }
                    if (position === this.length - 1) {
                        return this.tail.key;
                    }
                    let index = 0;
                    let current = null;
                    let len = Math.floor(this.length / 2);
                    if (len > position) {
                        current = this.head;
                        while (index++ < position) {
                            current = current.next;
                        }
                    } else {
                        current = this.tail;
                        index = this.length - 1;
                        while (index-- > position) {
                            current = current.prev;
                        }
                    }
                    return current.key;
                }
                has(key) {
                    let current = this.head;
                    while (current) {
                        if (current.key === key) {
                            return true;
                        }
                        current = current.next;
                    }
                    return false;
                }
                remove(key) {
                    let position = this.indexOf(key)
                    return this.removeAt(position)
                }
                removeAt(position) {
                    if (position < 0 || position > this.length - 1) {
                        return -1;
                    }
                    let current = this.head;
                    let index = 0;
                    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 {
                            while (index++ < position) {
                                current = current.next;
                            }
                            current.prev.next = current.next;
                            current.next.prev = current.prev
                        }
                    }
                    this.length -= 1
                    return current.key;
                }
                getHead() {
                    return this.head;
                }
                getTail() {
                    return this.tail;
                }
                size() {
                    return this.length;
                }
                isEmpty() {
                    return this.length === 0 ? true : false;
                }
                toString() {
                    return this.forwardString();
                }
            }
            let d = new DoubleLinkedList();
            d.append("AAA");
            d.append("BBB");
            d.append("CCC");
            d.insert(0, "AAA");
            d.insert(0, "BBB");
            d.insert(1, "CCC");
            d.insert(3, "DDD");
            // console.log("get", d.get(0));
            // console.log("get", d.get(6));
            // console.log("get", d.get(3));
            // console.log(d.indexOf("AAA"));
            // console.log("length", d.size());
            // console.log("isempty", d.isEmpty());
            // console.log("has", d.has("AAA"));
            // console.log("has", d.has("0"));
            // console.log("has", d.has("DDD"));
            // console.log("has", d.has("BBB"));
            console.log("removeAt", d.removeAt(0));
            console.log("removeAt", d.removeAt(1));
            console.log("removeAt", d.removeAt(4));
            console.log("removeAt", d.removeAt(3));
            console.log("removeAt", d.removeAt(2));
            console.log("length", d.size());
            console.log("remove ", d.remove('DDD'));
            console.log(d.toString());
            console.log(d);
        </script>
    </body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值