zoukankan      html  css  js  c++  java
  • js:数据结构笔记5--链表

    数组:

    • 其他语言的数组缺陷:添加/删除数组麻烦;
    • js数组的缺点:被实现为对象,效率低;
    • 如果要实现随机访问,数组还是更好的选择;

    链表:

    • 结构图:

    • 基本代码:
      function Node (elem) {
         this.elem = elem;
         this.next = null;
      }
      function LList() {
         this.head = new Node("head");
         this.find = find;
         this.insert = insert;
         this.findPrevious = findPrevious;
         this.remove = remove;
         this.display = display;
      }
      
      function find(item) {
         var currNode = this.head;
         while(currNode.elem !== item) {
            currNode = currNode.next;
         }
         return currNode;
      }
      function insert(newElem,item) {
         var newNode = new Node(newElem);
         var currNode = this.find(item);
         newNode.next = currNode.next;
         currNode.next = newNode;
      }
      function display() {
         var currNode = this.head;
         while(!(currNode.next === null)) {
            console.log(currNode.next.elem);
            currNode = currNode.next;
         }
      }
      function findPrevious(item) {
         var currNode = this.head;
         while(!(currNode.next === null) && (currNode.next.elem !== item)) {
            currNode = currNode.next;
         }
         return currNode;
      }
      function remove(item) {
         var prevNode = this.findPrevious(item);
         if(!(prevNode.next === null)) {
            prevNode.next = prevNode.next.next;
         }
      }

    操作:demo;

    双向链表:

    • 结构图:

    • 基本代码:
      function Node(elem) {
         this.elem = elem;
         this.next = null;
         this.previous = null;
      }
      function LList() {
         this.head = new Node("head");
         this.find = find;
         this.insert = insert;
         this.display = display;
         this.remove = remove;
         this.findLast = findLast;
         this.dispReverse = dispReverse;
      }
      
      function find(item) {
         var currNode = this.head;
         while(currNode.elem !== item) {
           currNode = currNode.next;
         }
         return currNode;
      }
      function insert(newElem,item) {
         var newNode = new Node(newElem);
         var currNode = this.find(item);
         newNode.next = currNode.next;
         newNode.previous = currNode;
         currNode.next = newNode;
      }
      function display() {
         var currNode = this.head;
         while(!(currNode.next === null)) {
            console.log(currNode.next.elem);
            currNode = currNode.next;
         }
      }
      function remove(item) {
         var currNode = this.find(item);
         if(!(currNode.next === null)) {
            currNode.previous.next = currNode.next;
            currNode.next.previous = currNode.previous;
            currNode.next = null;
            currNode.previous = null;
         }
      }
      function findLast() {
         var currNode = this.head;
         while(!(currNode.next === null)) {
            currNode = currNode.next;
         }
         return currNode;
      }
      function dispReverse() {
         var currNode = this.findLast();
         while(!(currNode.previous === null)) {
            console.log(currNode.elem);
            currNode = currNode.previous;
         }
      }

    操作:demo;

     循环链表:

    • 结构图:

  • 相关阅读:
    【数据结构】线性表&&顺序表详解和代码实例
    【智能算法】超详细的遗传算法(Genetic Algorithm)解析和TSP求解代码详解
    【智能算法】用模拟退火(SA, Simulated Annealing)算法解决旅行商问题 (TSP, Traveling Salesman Problem)
    【智能算法】迭代局部搜索(Iterated Local Search, ILS)详解
    10. js时间格式转换
    2. 解决svn working copy locked问题
    1. easyui tree 初始化的两种方式
    10. js截取最后一个斜杠后面的字符串
    2. apache整合tomcat部署集群
    1. apache如何启动
  • 原文地址:https://www.cnblogs.com/jinkspeng/p/4030681.html
Copyright © 2011-2022 走看看