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;
        };
    }
  • 相关阅读:
    Coursera Algorithm II PA2 Q2
    Coursera Algorithm Part II PA2
    实现 memcpy 函数
    超人
    Proxy 模式
    【6】锋利的 jQuery 笔记
    【3】Chrome 的一些常用操作
    HTML 待解决与已解决问题
    CSS 待解决问题
    JS 一些常用技巧
  • 原文地址:https://www.cnblogs.com/shanchenba/p/5680893.html
Copyright © 2011-2022 走看看