美文网首页
14. 邻接链表相关简单api封装

14. 邻接链表相关简单api封装

作者: wudimingwo | 来源:发表于2018-12-09 22:10 被阅读0次
  1. 创建节点
      function CreateNode (key) {
        this.key = key;
        this.next = null;
      }
      
      let node1 = new CreateNode(1);
      let node2 = new CreateNode(2);
      let node3 = new CreateNode(3);
      let node4 = new CreateNode(4);
  1. 尾插,
    我们只需要知道头节点, 就可以找到最后一个节点,
    就可以找到任何一个节点
     function insert (node,lastnode) {
          
          while (node.next){
            node = node.next;
          }
          node.next = lastnode;
     }
     insert(node1,new CreateNode(2));
     insert(node1,new CreateNode(3));
     insert(node1,new CreateNode(4));
     insert(node1,new CreateNode(5));
  1. 有头节点
     let head = {type : "head",next : null};
     function insert (head,lastnode) {
          
          while (head.next){
            head = head.next;
          }
          head.next = lastnode;
     }
     insert(head,new CreateNode(2));
     insert(head,new CreateNode(3));
     insert(head,new CreateNode(4));
  1. 任意位置插入
     let head = {type : "head",next : null};
     function insert (head,node,num = Infinity) {
          if (nun < 1) {
            return
          }
          while (head.next && --num > 0){
            head = head.next;
          }
          // 先临时保存
          let nextnode = head.next;
          // 插入新节点, 此时与原来的next 断开了
          head.next = node;
          // 重新接上.
          node.next = nextnode;
     }
  1. 删除
根据节点删除
     function deleteNode (head,node) {
        while (head.next){
            if (head.next == node) {
                head.next = head.next.next;
                  return
            }
            head = head.next;
        }
     }

根据节点值删除
     function deleteNode (head,key) {
        while (head.next){
            if (head.next.key === key) {
                head.next = head.next.next;
                    return 此处不return 就是把链表中所有符合条件都干掉
                              此处return 就表示只干掉第一个满足条件的节点
            }
            head = head.next;
        }
     }

根据位置删除? 但实际上位置是相对来讲动态的.
     function deleteNode (head,num) {
       if (num < 1) {
        return
       }
        while (head.next && num-- > 0){
            if (num === 0) {
                head.next = head.next.next;
                return
            }
            head = head.next;
        }
     }
  1. 判断有没有环
  • 取一个步长为一, 取一个步长为二 的 指针
  • 如果出现两个指针相同, 且不为空, 则表明形成环了.
  • 好聪明, 这就像是运动场上的跑步
     function findRing (head) {
        let point1 = head.next;
        let point2 = point1 && point1.next;// head.next.next 有可能报错
        
        while (point1 && point2){ // point1 point1 都存在且不为空
            if (point1 === point2) {
                return true
            }
            point1 = point1.next;
            point2 = point2.next;
            point2 = point2 && point2.next;
        }
        return false;
     }

相关文章

  • 14. 邻接链表相关简单api封装

    创建节点 尾插,我们只需要知道头节点, 就可以找到最后一个节点,就可以找到任何一个节点 有头节点 任意位置插入 删...

  • 5 图的复习目录

    5.1 图 5.2 图的存储结构 邻接矩阵 邻接表 十字链表 邻接多重链表 5.3 图的遍历 深度优先 广...

  • 【算法】基本的图算法

    图的搜索技巧是整个图算法领域的核心。 图的表示 有两种标准方法:1.邻接链表2.邻接矩阵 邻接链表是将一个结点所有...

  • 图(邻接链表)(C语言)

    邻接链表实现图 队列头文件queue.h

  • 面试准备之【数据结构】1——图

    一. 有向图/无向图 共有:邻接表,邻接矩阵 有向图独有:十字链表,边集数组 无向图独有:邻接多重表 1.邻接矩...

  • 邻接表及其实现

    图的邻接表表示法类似于树的孩子链表表示法。 编辑文件信息 打印邻接表。

  • Kandroid代码篇 (1) 简单Logger封装

    简单 Logger 封装需求kotlin 封装实现kotlin 相关知识点 一、简单 Logger 封装需求   ...

  • 基本的数据结构有哪些

    图: 有向图:无向图: 图的存储结构:1,邻接矩阵(数组表达)2,邻接表和十字链表,链表表达,主要表达有向图3,邻...

  • 算法

    1.图的存储结构 邻接矩阵表示法 便于运算邻接表表示法 对于稀疏图来讲,更节省存储空间十字链表邻接多重表 ...

  • 图的存储: 邻接矩阵 邻接链表 链式前向星 = 边集数组+邻接表 链式前向星代码,维护一个head头数组,以及一个...

网友评论

      本文标题:14. 邻接链表相关简单api封装

      本文链接:https://www.haomeiwen.com/subject/oxtlhqtx.html