• JS 链表


    链表

    定义

    • 是一组节点的集合
    • 每个节点都使用一个对象的引用来指向的后继
    • 每个节点的引用叫做链表

    和数组的不同

    • 数组靠它的位置来指向他的引用
    • 链表则靠他们相互之间的关系进行引用

    链表的抽象定义

    • 元素值
    • 指向下一个节点的引用
    • 指向上一个节点的引用

    双向链表的实现

    function Node(element) {
        this.element = element;
        // 下一节点
        this.next = null;
        // 上一节点
        this.prev = null;
    }
    
    
    function Linklist() {
        this.head = new Node("head");
    
        this.insert = insert;
        this.remove = remove;
        this.find = find;
        this.display = display;
        this.findPreNode = findPreNode;
    }
    
    function find(element) {
        var curNode = this.head;
        console.log(curNode);
        while (curNode.element != element) {
            curNode = curNode.next;
        }
        return curNode;
    }
    
    function insert(newEl, oldEl) {
        var newNode = new Node(newEl);
    
        var curNode = this.find(oldEl);
        newNode.next = curNode.next
        newNode.preNode = curNode;
        curNode.next = newNode;
    
    
    }
    
    
    function display() {
        var headNode = this.head;
        var resMsg = "";
        while (headNode.next != null) {
            resMsg += headNode.element + ",";
            headNode = headNode.next;
        }
        return resMsg;
    }
    
    
    function remove(element) {
        var curNode = this.find(element);
        if (curNode != null) {
            curNode.prev.next = curNode.next;
            curNode.next.prev = curNode.prev;
            curNode.prev = null;
            curNode.next = null;
        }
    }
    
    function findPreNode(element) {
        var curNode = this.head;
        while (curNode.next != null && curNode.next.element != element) {
            curNode = curNode.next;
        }
        return curNode;
    }
    
    function findLast() {
        var curNode = this.head;
        while (curNode.next != null) {
            curNode = curNode.next;
        }
        return curNode;
    }
    

    循环列表,将头节点的

    this.head.next=this.head;
  • 相关阅读:
    qt QTimer 计时器
    qt DateTime 计算时间
    c++ win32 关机 注销 重启
    uniapp 修改meta:viewport
    初次使用 VUX
    移动端web app自适应布局探索与总结
    前端js模版 预编译工具Tmod js使用入门
    谷歌 Uncaught SecurityError: Failed to execute 'replaceState' on 'History 错误
    H5 前端页面适配响应式
    微信video标签全屏无法退出bug 本文系转载
  • 原文地址:https://www.cnblogs.com/dark-liu/p/5796919.html
Copyright © 2020-2023  润新知