[Javascript Data Structures] LinkedList 链表

(1) 单向链表

就是对象之间的连接, 对象的属性有data & next指针(指向下一个node)

初始化head=null;
length=0; // 链表长度为0

var length=0;//链表长度为0
var head=null;//没有头结点

这里写图片描述


[1-1]加入Node

//add data into the linkedlist
      this.append=function(data){
         var node=new Node(data);
         if (head===null) {
              //如果没有head 那么将node作为头结点
              head=node;

         }else{
             var current=head;
             //从头结点开始找到最后能插入的位置
             while(current.next){//true
                current=current.next;
             }//找到了最后的位置
             current.next=node;
         }
         length++;          
      }

这里写图片描述


[1-2] 完整代码

<script type="text/javascript">
   function LinkedList(){
      //初始化节点
      function Node(data){
      //节点有 数据和指针
          this.data=data;
          this.next=null;
      }
      var length=0;//链表长度为0
      var head=null;//没有头结点


//[1] print the linkedlist
      this.print=function(){
          if (head===null) {
              console.log("LinkedList without data");
          }else{
              //当前指针指向head
              var current=head;
              while(current){//true
                 console.log(current.data);
                 //指向下一个node
                 current=current.next;
              }
          }
      }

//[2] add data into the linkedlist
      this.append=function(data){
         var node=new Node(data);
         if (head===null) {
              //如果没有head 那么将node作为头结点
              head=node;

         }else{
             var current=head;
             //从头结点开始找到最后能插入的位置
             while(current.next){//true
                current=current.next;
             }//找到了最后的位置
             current.next=node;
         }
         length++;          
      }

//[3] remove the data from the linkedlist     
      this.remove=function(index){
         //index 是从0-length-1
         //如果越界了或者不是数字
         if (typeof index!='number'||index<0||index>length) {
              console.log("Not a number || Beyond the scope");
         }else{
            var current=head;//指针从头开始

            if (index===0) {
                head=current.next;
            }else{
                for (var i = 1; i < length; i++) {
                   if (index===i) {//找到了要替换的位置
                      current.next=current.next.next;
                   }else{
                      current=current.next;//指向下一个位置
                   }
                }
            }
                  length--;
         }
      }

//[4] insert the node
      this.insert=function(index,data){
        var node=new Node(data);

        if (typeof index!='number'||index<0||index>length) {
            console.log("Not a number || Beyond the scope");
        }else{
            var current=head;
            //要插入头后面
             if (index===0) {
                node.next=current;
                head=node;//新加入点 现在为新的head
             }else{ 
               for (var i = 1; i < length; i++) {                   
                   if (index===i) {
                       node.next=current.next;
                       current.next=node;
                   }else{
                       current=current.next;
                   }
               }
            }
           length++; 
        }
      }


//[5] get指定位置值
      this.getindex=function(index){
          if (typeof index!='number'||index<0||index>length) {
            console.log("Not a number || Beyond the scope");
          }else{
            var current=head;
            if (index===0) {
                console.log(current.data);
            }else{
                for (var i = 1; i < length; i++) {
                    if (index===i) {
                       console.log(current.next.data);
                    }else{
                       current=current.next;
                    }
                }
            }
          }         
      }


//[6] this.getsize=function(){
         console.log("the size of linkedlist:"+length);
      }

  }


//Test
   var linkedlist=new LinkedList();
   console.log("\nadd data:");
   linkedlist.append("1jessica");
   linkedlist.append("2krystal");
   linkedlist.append("3yoonA");
   linkedlist.append("4ljy");
   linkedlist.print();


   console.log("\nremove data:");
   linkedlist.remove(0);
   linkedlist.print();

   console.log("\ninsert data:");
   linkedlist.insert(2,"GG");
   linkedlist.print();

   console.log("\nget data:");
   linkedlist.getindex(1);

   console.log("\nget size:");
   linkedlist.getsize();
</script>

Result:
这里写图片描述


(2) 双向列表

this.data=data;
this.next=null;
this.prev=null;//*******different

head=null;
length=0;
trail=null;//*******different—save the last .node

<script type="text/javascript">
   function DoubleLinkedList(){
      function Node(data){
         this.data=data;
         this.next=null;
         this.prev=null;//*******different 
      }
      head=null;
      length=0;
      trail=null;//*******different---save the last .node



//[1] add the node into the list
      this.append=function(data){
         var node=new Node(data);

         if (head===null) {
              //如果没有head 那么将node作为头结点
              head=node;
              /*different*/
              trail=node;
              /*different*/

         }else{
             var current=head;
             //从头结点开始找到最后能插入的位置
             while(current.next){//true
                current=current.next;
             }//找到了最后的位置
              current.next=node;
              trail=node;
         }
         length++;          
      }


//[2] print the doublelinkedlist
      this.print=function(){
          if (head===null) {
              console.log("DoubleLinkedList without data");
          }else{
              //当前指针指向head
              var current=head;
              while(current){//true
                 console.log(current.data);
                 //指向下一个node
                 current=current.next;
              }
          }
      }

//[3] insert the doublelinkedlist
      this.insert=function(index,data){
          var node=new Node(data);
          if (head===null) {
            //新增node = head
            head=node;
            tail=node;//******* different 
          }else if(index===0){
            //插到head之前 node变为新的head
            node.next=head;//node 之后是 head
            head.prev=node;//head 之前是node
            head=node;//再改变node为head
          }else if(index===length){//最后一项
           //******* different
            trail.next=node;
            node.prev=trail;
            trail=node;  
           //******* different     
          }else if(index>0||index<length){
            var current=head.next;
            var previous=head; 
            for (var i = 1; i < length; i++) {                    
              if (index===i) {
                 //******* different
                  node.next=current;
                  previous.next=node;
                  current.prev=node;
                  node.prev=previous;
                 //******* different
                  break;
              }else{
                  previous=current;  
                  current=current.next;
               }
            }
          }
          length++;
      }
   }

   var doublelinkedlist=new DoubleLinkedList();
   console.log("\nadd data:");
   doublelinkedlist.append("1jessica");
   doublelinkedlist.append("2krystal");
   doublelinkedlist.print();

   console.log("\ninsert data:");
   doublelinkedlist.insert(2,"AAAAAA");
   doublelinkedlist.print();
</script>

Result:

这里写图片描述

这里写图片描述


(3) 循环列表

这里写图片描述

头尾相连
head.prev=trail;
trail.next= head;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值