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

      

  • 相关阅读:
    201706120024 编译原理第一次作业
    P2014 选课 题解(树形DP)
    基础算法·二分答案
    P4285 [SHOI2008]汉诺塔 题解 (乱搞)
    2018.12-2019.1 TO-DO LIST
    记录一枚蒟蒻的成长(持续更新)
    P3796 【模板】AC自动机(加强版) 题解(Aho-Corasick Automation)
    BuaacodingT651 我知道你不知道圣诞节做什么 题解(逻辑)
    P2766 最长不下降子序列问题 题解(网络流)
    P2516 [HAOI2010]最长公共子序列 题解(LCS)
  • 原文地址:https://www.cnblogs.com/BaiGuodong/p/5575879.html
Copyright © 2020-2023  润新知