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

      

  • 相关阅读:
    信号处理引发的cpu高
    两个混淆的用户锁定
    一段获取权限的脚本
    AF_INET 和PF_INET区别;AF_LOCAL PF_LOCAL 区别.
    一个三目运算符问题
    nginx cpu高排查
    mmap 测试的一些坑
    哈希——布隆过滤器 查黑名单(大数据 100亿数据)
    哈希——设计RandomPool结构
    哈希
  • 原文地址:https://www.cnblogs.com/BaiGuodong/p/5575879.html
Copyright © 2011-2022 走看看