zoukankan      html  css  js  c++  java
  • 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;
            }
    }
    

      

  • 相关阅读:
    三元运算符嵌套问题
    tp框架利用redis存储session
    PHP date('W')遇到跨年问题
    AES加解密 集成 spring MVC
    python SMTP 发送邮件
    mysql数据库安装过程
    (转)Apache启动过程
    apache服务器全局配置详解
    Apache 的 httpd.conf 详解(很实用)
    安装apache遇到的问题总结
  • 原文地址:https://www.cnblogs.com/BaiGuodong/p/5575879.html
Copyright © 2011-2022 走看看