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;
        }
}

  

posted @ 2016-06-11 20:37  白不菜  阅读(403)  评论(0编辑  收藏  举报