zoukankan      html  css  js  c++  java
  • 原生JS实现单向链表

    1.前言

    用JS实现一个简单的单向链表,并完成相关的功能

    2.功能说明

    1. push(value):从链表尾部添加一个新的节点
    2. insertAfer(value,item):向链表中的item节点之后插入一个 值为value的新节点
    3. remove(value):删除链表中值为value的节点
    4. removeAt(pos):删除链表中第pos个节点
    5. find(value):查找链表中值为value的节点
    6. findPrevious(value):查找链表中值为value的节点的前一个节点
    7. indexof(vallue):查找链表中值为value的节点的索引值,如果查找不到则返回-1
    8. size():获取当前链表的长度
    9. getHead():获取当前链表的头节点
    10. print():打印当前链表,供测试用

    3. 代码实现

    3.1 创建链表类

     1 //创建一个Node辅助类,用来生成节点
     2 function Node(value) {
     3     this.value = value;
     4     this.next = null;
     5   }
     6   
     7 //链表类
     8 function LinkedList() {
     9     this.head = null;
    10     this.length = 0;
    11     //向链表尾部追加元素
    12     this.push = push;
    13     //从链表中查找某个元素
    14     this.find = find;
    15     //在链表中任意一个元素之后插入一个元素
    16     this.insertAfter = insertAfter;
    17     //从链表中查找任意元素节点的前一个节点
    18     this.findPrevious = findPrevious;
    19     //从链表中删除值为value的元素
    20     this.remove = remove;
    21     //返回当前链表的长度
    22     this.size = size;
    23     //查找某个元素在链表中的索引值
    24     this.indexof = indexof;
    25     //删除链表中第pos个元素
    26     this.removeAt = removeAt;
    27     //获取链表中第一个元素
    28     this.getHead = getHead;
    29     //打印当前的链表,供测试用
    30     this.print = print;
    31   }

    2.1 push(value):从链表尾部添加一个新的节点

    function push(value) {
        var node = new Node(value);
        if (this.head == null) {
          this.head = node;
        } else {
          var current = this.head;
          while (current.next != null) {
            current = current.next;
          }
          current.next = node;
        }
        length++;
      }

    3.3 insertAfer(value,item):向链表中的item节点之后插入一个 值为value的新节点

    function insertAfter(value, item) {
        var node = new Node(value);
        var current = this.find(item);
        if (current == null) {
          return console.log('找不到元素');
        }
        node.next = current.next;
        current.next = node;
        length++;
      }

    3.4 remove(value):删除链表中值为value的节点

    function remove(value) {
      var current = this.find(value);
      if (!current) {
        return console.log('链表中找不到被删除的元素');
      }
      var previous = this.findPrevious(value);

      if (!previous) {
        this.head = current.next;
      } else {
        previous.next = current.next;
      }
      this.length--;
    }

    3.5 removeAt(pos):删除链表中第pos个节点

    function removeAt(pos) {
        if (pos > -1 && pos < length) {
          var current = this.head;
          var index = 0;
          if (pos === 0) {
            this.head = current.next;
          } else {
            while (index < pos) {
              var previous = current;
              current = current.next;
              index++;
            }
            previous.next = current.next;
          }
          length--;
        } else {
          return null;
        }
      }

    3.6 find(value):查找链表中值为value的节点

    function find(value) {
        var currentNode = this.head;
        if (currentNode == null) {
          console.log("这是一个空链表!!!");
          return null;
        }
        if (currentNode.value === value) {
          return currentNode;
        }
        while (currentNode.next) {
          currentNode = currentNode.next;
          if (currentNode.value === value) {
            return currentNode
          }
        }
        console.log("没有找到该元素!!!");
        return null;
      }

    3.7 findPrevious(value):查找链表中值为value的节点的前一个节点

    function findPrevious(value) {
      var current = this.head;
      if (current == null) {
        console.log('这是一个空链表');
        return null;
      }
      if (current.value === value) {
        console.log('当前查找的节点为链表的头节点,头节点前再无节点');
        return null;
      }
      while (current.next) {
        if (current.next.value === value) {
          return current;
        }
        current = current.next;
      }
      console.log('找不到该元素的前一个元素');
      return null;
    }

    3.7 indexof(vallue):查找链表中值为value的节点的索引值,如果查找不到则返回-1

    function indexof(value) {
        var current = this.head;
        var index = 0;
        if (current == null) {
          return null;
        } else {
          while (current) {
            if (current.value === value) {
              return index;
            }
            index++;
            current = current.next;
          }
        }
        return -1;
      }

    3.8 size():获取当前链表的长度

    function size(){
        return length;
      }

    3.9 getHead():获取当前链表的头节点

    function getHead(){
        return this.head;
      }

    3.10 print():打印当前链表,供测试用

    function print() {
        var current = this.head;
        while (current != null) {
          console.log(current.value);
          current = current.next;
        }
      }

    4. 功能测试

    var list = new LinkedList();
      for (var i = 1; i < 6; i++) {
        list.push(i);
      }
    list.print();

    5.完整代码

    完整代码请戳☞☞☞LinkedList.js

    (完)

  • 相关阅读:
    [eclipse]如何修改Eclipse编辑器的字体
    评CSDN上一篇讲述数据迁移的文章“程序员 12 小时惊魂记:凌晨迁移数据出大事故!”
    [Java]算术表达式组建二叉树,再由二叉树得到算式的后序和中序表达式
    [Java]算术表达式求值之三(中序表达式转二叉树方案 支持小数)
    [Java]手动构建表达式二叉树,求值,求后序表达式
    转载:构建语法树来解析数学表达式
    Oracle:Enterprise Manager 无法连接到数据库实例。下面列出了组件的状态。 以及 Oracle11g OracleDBConsoleorcl服务无法启动问题
    一个完整的企业Java项目的生命周期
    安装最新版本的Oracle公司的虚拟机软件 VirtualBox + 安装虚拟机 Windows XP 系统 + 安装 Oracle 11g 软件 + 出现 ERROR: ORA-12541: TNS:no listener 错误解决 + Oracle 11g数据库详细“卸载”步骤
    js的跨域问题 和 jQuery的跨域问题
  • 原文地址:https://www.cnblogs.com/wangjiachen666/p/9462895.html
Copyright © 2011-2022 走看看