js实现链表

427 阅读1分钟

单向链表 js里的链表指向上一个元素/下一个元素体现为级联关系 next存放指向下一个节点的属性 用对象实现js链表

// 辅助类 生成节点

class Node {  
    constructor(element) {
        this.element = element;
        this.next = null;
    }
}

链表操作:

  • append
  • getNode
  • appendAt
  • remove
class linkList {
    constructor() {
        this.size = 0;
        this.head = null;
    }
    append(element) {
        let node = new Node(element);
        if (this.head == null) {
            this.head = node;
        } else {
            // 根据长度找到最后一个
            let current = this.getNode(this.size - 1);
            current.next = node;
        }
        this.size++
    }
    getNode(index) {
            if (index < 0 || index > this.size) {
                throw new Error('error')
            }
            let current = this.head;
            for (let i = 0; i < index; i++) {
                current = current.next
            }
            return current
        }
        // 向指定位置添加 在指定位置之后
    appendAt(position, elemnt) {
            if (position < 0 || position >= this.size) {
                throw new Error('error')
            }
            let node = new Node(elemnt);
            if (position == 0) {
                node.next = this.head;
                this.head = node;
            }
            if (position < this.size) {
                let current = this.getNode(position); //找到最初位置上的值
                let pre = this.getNode(position - 1);
                pre.next = node;
                let now = this.getNode(position);
                console.log(current, 'current', pre, 'pre', now);
                node.next = current;
            }
        }
        //移除l
    remove(position) {
            if (position < 0 || position >= this.size) {
                throw new Error('error')
            }
            let pre = this.getNode(position - 1);
            let current = this.getNode(position);
            pre.next = current.next
            this.size--
        }
        // 查找指定元素
    indexof(element) {
        let current = this.head;
        for (var i = 0; i < this.size; i++) {
            if (current.element == element) {
                return i + 1;
            }
            current = current.next;
            console.log(current, '-1');
        }
        return -1
    }
}

单向循环链表

class Node {
    constructor(element) {
        this.element = element;
        this.next = null;
    }
}
class CircularLinkedList {
    constructor() {
        this.size = 0;
        this.head = null;
    }
    append(element) { // 插入
        let node = new Node(element)
        if (this.head == null) {
            this.head = node;
            node.next = this.head;
            return;
        }
        let current = this.head;
        while (current.next != this.head) { // 找到最后一项
            current = current.next;
        }
        this.size++;
        current.next = node;
        node.next = this.head;
    }
    delete(position) { // 删除指定位置
            if (position < 0) {
                throw new Error('error错误')
            }
            if (position == 0) {
                this.head = this.head.next;
                return
            }
            let index = 0;
            let current = this.head;
            let pre;
            while (index++ < position) {
                pre = current;
                current = current.next;
            }
            pre.next = current.next;
        }
        // 获取指定位置的元素
    getNode(position) {
        let current = this.head;
        let index = 0;
        while (index++ < position) { // 找到最后一项
            current = current.next;
        }
        return current;
    }
    appendAt(position, element) { // 向指定位置插入
            if (position < 0) {
                throw new Error('------传值错误')
            }
            let index = 0;
            let pre;
            let node = new Node(element)
            let current = this.head;
            while (index++ < position) { // 找到最后一项
                pre = current;
                current = current.next;
            }
            pre.next = node;
            node.next = current;
            this.size++;
        }
        // 移除指定元素
    deletElement(element) {
        let current = this.head;
        let pre;
        let index = 0;
        while (index < this.size) {
            if (pre.element == element) {
                pre.next = current.next;
                this.size--
            } else {
                pre = current;
                current = current.next;
                index++;
            }

        }
    }
}

let node = new CircularLinkedList();
let nodes = [1, 2, 3, 4, 5];
nodes.forEach(element => {
    node.append(element);
});
console.dir(node, { depth: 100 });
// node.delete(0); // node.delete(2);
// console.log('----------------');
// console.dir(node, { depth: 100 });
// console.log('----------------', node.getNode(1));
// node.appendAt(1, 11)
node.deletElement(2)
console.dir(node, { depth: 100 });