Javascript中的链表

Posted 白菜园

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javascript中的链表相关的知识,希望对你有一定的参考价值。

function LinkedList() {
        // 辅助类,表示加入链表的每一项
        var Node=function(element){
          this.element=element;
          this.next=null;
        }
        // 列表,存储链表的长度
        var length=0;
        var head=null;
        this.append=function(element){
          var node=new Node(element),current;
          if(head===null){
            head=node;
          }else{
            current=head;
            // 找到链表中的最后一个元素
            while (current.next) {
              current=current.next;
            }
            current.next=node;
          }
          length++;
        };
        this.insert=function(pos,element){
          if (pos>=0 && pos<=length) {
            var node=new Node(element),current=head,previous,index=0;
            if(position===0){
              node.next=current;
              head=node;
            }else{
              while (index++<pos) {
                previous=current;
                current=current.next;
              }
              node.next=current;
              previous.next=node;
            }
            length++;
            return true;
          }else{
            return false;
          }
        };
        this.removeAt=function(pos){
          if(pos>-1 && pos<length){
            var current=head,previous,index=0;
            if(pos==0){
              head=current.next;
            }else{
              while (index++<pos) {
                previous=current;
                current=current.next;
              }
              previous.next=current.next;
            }
            length--;
            return current.element;
          }else{
            return null;
          }
        };
        this.remove=function(element){
          var index=this.indexOf(element);
          return this.removeAt(index);//不太好,重复遍历了2次
        };
        this.indexOf=function (element) {
          var current=head,index=0;
          while (current) {
            if(element===current.element){
              return index;
            }
            index++;
            current=current.next;
          }
          return -1;
        };
        this.isEmpty=function () {
          return length===0;
        };
        this.size=function () {
          return length;
        };
        this.toString=function(){
          var current=head,str=‘‘;
          while (current) {
            str+=","+current.element;
            current=current.next;
          }
          return str.slice(1);
        };
        this.print=function(){
          console.log(this.toString());
        };
        this.getHead=function(){
          return head;
        }
      }
      function DoublyLinkedList() {
        // 辅助类,表示加入链表的每一项
        var Node=function(element){
          this.element=element;
          this.next=null;
          this.prev=null;
        }
        // 列表,存储链表的长度
        var length=0;
        var head=null;
        var tail=null;
        this.append=function(element){
          var node=new Node(element),current;
          if(head===null){
            head=node;
          }else{
            current=head;
            // 找到链表中的最后一个元素
            while (current.next) {
              current=current.next;
            }
            current.next=node;
          }
          length++;
        };
        this.insert=function(pos,element){
          if (pos>=0 && pos<=length) {
            var node=new Node(element),current=head,previous,index=0;
            if(pos===0){
              if(!head){
                head=node;
                tail=node;
              }else{
                node.next=current;
                current.prev=node;
                head=node;
              }
            }else if(pos==length){
              current=tail;
              current.next=node;
              node.prev=current;
              tail=node;
            }else{
              while (index++<pos) {
                previous=current;
                current=current.next;
              }
              node.next=current;
              node.prev=previous;
              previous.next=node;
              current.prev=node;
            }
            length++;
            return true;
          }else{
            return false;
          }
        };
        this.removeAt=function(pos){
          if(pos>-1 && pos<length){
            var current=head,previous,index=0;
            if(pos==0){
              head=current.next;
              if(!length===1){
                tail=null;
              }else{
                head.prev=null;
              }
            }else if(pos==length-1){
              current=tail;
              tail=current.prev;
              tail.next=null;
            }else{
              while (index++<pos) {
                previous=current;
                current=current.next;
              }
              previous.next=current.next;
              current.next.prev=current.prev;
            }
            length--;
            return current.element;
          }else{
            return null;
          }
        };
        this.remove=function(element){
          var index=this.indexOf(element);
          return this.removeAt(index);//不太好,重复遍历了2次
        };
        this.indexOf=function (element) {
          var current=head,index=0;
          while (current) {
            if(element===current.element){
              return index;
            }
            index++;
            current=current.next;
          }
          return -1;
        };
        this.isEmpty=function () {
          return length===0;
        };
        this.size=function () {
          return length;
        };
        this.toString=function(){
          var current=head,str=‘‘;
          while (current) {
            str+=","+current.element;
            current=current.next;
          }
          return str.slice(1);
        };
        this.print=function(){
          console.log(this.toString());
        };
        this.getHead=function(){
          return head;
        }
}

  

以上是关于Javascript中的链表的主要内容,如果未能解决你的问题,请参考以下文章

javascript中的链表结构—双向链表

为啥我的链表代码在模块化可重用 JavaScript 函数中不起作用?

javascript中的链表结构

javascript中的链表结构

javascript中的链表结构—从链表中删除元素

Software-DataStructure 三年一直迷糊的链表