zoukankan      html  css  js  c++  java
  • 链表

    // 单向链表
    function LinkedList() {
        this.head = {element: 'head', next: null};
        this.node = function (node) {return {element: node, next: null}};
    
        // 查找
        this.find = function (node) {
            var currNode = this.head;
            while (currNode !== null && currNode.element !== node) {
                currNode = currNode.next;
            }
            return currNode;
        };
    
        // 输出
        this.show = function () {
            var currNode = this.head;
            while (currNode.next !== null) {
                currNode = currNode.next;
                console.log(currNode.element);
            }
        };
    
        // 插入
        this.insert = function (newNode, targetNode) {
            var newNode = this.node(newNode);
            var targetNode = this.find(targetNode);
            newNode.next = targetNode.next;
            targetNode.next = newNode;
        };
    
        // 上一个节点
        this.prev = function (node) {
            var currNode = this.head;
            while (currNode.next !== null && currNode.next.element !== node) {
                currNode = currNode.next;
            }
            return currNode;
        };
    
        // 删除
        this.remove = function (node) {
            var prevNode = this.prev(node);
            prevNode.next = prevNode.next.next;
        };
    }
  • 相关阅读:
    Jenkins安装
    Python操作yaml文件
    class 中构造函数与析构函数
    python发送邮件(yagmail模块)
    filter、map函数的区别
    python redis操作
    多个 python的pip版本选择
    python Excel操作
    python MD5操作
    缓存淘汰算法之LRU实现
  • 原文地址:https://www.cnblogs.com/shanchenba/p/5680893.html
Copyright © 2011-2022 走看看