Js实现链表

1.链表的插入(尾插法)

  1. 图形描述过程
    在这里插入图片描述

  2. 代码实现

function LinkedList(){

     //节点类
     function Node(data){
         this.data=data;
         this.next=null;
     }

     //属性
     this.head=null;//空链表时head指向null
     this.length=0;//链表长度

     //向链表尾部添加一个新的项(尾插法)
     LinkedList.prototype.append=function(data){
         //创建一个新节点
         var newNode=new Node();
         //如果要插入的是第一个链表,让head指向第一个节点
         if(this.length==0){
             this.head=newNode;
         }
         //如果不是第一个节点,则找到链表尾部的最后一个节点,让这个节点指向要插入的节点
         else{
             var current=this.head;//此时current指向第一个节点
             while(current.next){
                  current=current.next;
             }
             current.next=newNode;
         }
         //插入节点后,链表长度+1
         this.length+=1;
     }

   }

2.链表特定位置插入一个新的项

LinkedList.prototype.insert=function(position,data){
         //判断position是否合法
         if(position<0||position>this.length){
             return false;
         }
         //创建一个新节点
         var newNode=new Node(data);
         //要插入的位置是第一个
         if(position==0){
             newNode.next=this.head;
             this.head=newNode;
         }
         //要插入的位置不是第一个,寻找要插入的位置,previous指向其前一个节点,current指向插入位置的节点,让新节点指向current,previous指向新节点
         else{
            var index=0;
            var current=this.head,previous=null;
            while(index++<position){
                previous=current;
                current=current.next;
            }
            newNode.next=current;
            previous.next=newNode;
         }
         //节点插入成功,节点个数+1
         this.length+=1;
         return true;
         
     }

3.获取对应位置的元素

LinkedList.prototype.get=function(position){
         //判断position合法,因为position是从0开始的,举个例子,有0 1 2 ,此时length=3,position不能大于等于3
         if(position<0||position>=this.length){
             return false;
         }
         //找位置
         var current=this.head;
         var index=0;//从位置0开始找
         //index最终到position前一个位置,current最终指向position位置的节点
         while(index++<position){
             current=current.next;
         }
         //找到后返回元素
         return current.data;
     }

4.返回元素在链表中的索引,如果链表中没有,返回-1

LinkedList.prototype.indexOf=function(data){
         var current=this.head;
         var index=0//默认链表节点索引从0开始
         var c=0
         while(current){
             if(current.data==data){
                 return index;
             }
             else{
                current=current.next;
                index+=1;
             }
         }
         //匹配玩所有的节点,都没找到
         return -1;
     }

5.修改某个位置的元素

LinkedList.prototype.update=function (position,data){
        //判断position合法,因为position是从0开始的,举个例子,有0 1 2 ,此时length=3,position不能大于等于3
        if(position<0||position>=this.length){
             return false;
         }
         //找位置
         var current=this.head;
         var index=0;//从位置0开始找
         //index最终到position前一个位置,current最终指向position位置的节点
         while(index++<position){
             current=current.next;
         }
         //找到后更改元素
         current.data=data;
         return true;
    }

6.从链表的特定位置删除一项

LinkedList.prototype.removeAt=function(position){
         //判断position合法性
         if(position<0||position>=this.length){
             return false;
         }
         //如果删除的是第一个元素,让head直接指向它的next
         if(position==0){
             this.head=this.head.next;
         }
         //不是第一个元素时,找到要删除的位置的节点由current指向,要删除位置的前一个节点由previous指向,让previous.next指向current.next
         else{
             var current=this.head;
             var previous=null;
             var index=0;
             while(index++<position){
                 previous=current;
                 current=current.next;
             }
             previous.next=current.next;
         }
         //删除节点成功,节点数-1
         this.length-=1;
         return true;
     }

7.从链表中移除一项

//通过indexOf()方法找到对应位置,然后根据位置删除对应项removeAt(position);
     LinkedList.prototype.remove=function(data){
         var position=this.indexOf(data);
         return this.removeAt(position);
     }

8.链表是否为空

LinkedList.prototype.IsEmpty=function(){
        return this.length?false:true;
    }

9.返回链表的个数

 LinkedList.prototype.Size=function(){
        return this.length;
    }

10.将链表以字符串的形式输出

LinkedList.prototype.toString=function(){
        var str='';
        var current=this.head;
        while(current) {
            str+=current.data+' ';
            current=current.next;
        }
        return str;    
        }

对于单链表的基本操作就是这些了,下面是这些方法的汇合:

//链表
   
   function LinkedList(){

     //节点类
     function Node(data){
         this.data=data;
         this.next=null;
     }

     //属性
     this.head=null;//空链表时head指向null
     this.length=0;//链表长度

     //向链表尾部添加一个新的项(尾插法)
     LinkedList.prototype.append=function(data){
         //创建一个新节点
         var newNode=new Node(data);
         //如果要插入的是第一个链表,让head指向第一个节点
         if(this.length==0){
             this.head=newNode;
         }
         //如果不是第一个节点,则找到链表尾部的最后一个节点,让这个节点指向要插入的节点
         else{
             var current=this.head;//此时current指向第一个节点
             while(current.next){
                  current=current.next;
             }
             current.next=newNode;
         }
         //插入节点后,链表长度+1
         this.length+=1;
     }


     //向链表的特定位置插入一个新的项
     LinkedList.prototype.insert=function(position,data){
         //判断position是否合法
         if(position<0||position>this.length){
             return false;
         }
         //创建一个新节点
         var newNode=new Node(data);
         //要插入的位置是第一个
         if(position==0){
             newNode.next=this.head;
             this.head=newNode;
         }
         //要插入的位置不是第一个,寻找要插入的位置,previous指向其前一个节点,current指向插入位置的节点,让新节点指向current,previous指向新节点
         else{
            var index=0;
            var current=this.head,previous=null;
            while(index++<position){
                previous=current;
                current=current.next;
            }
            newNode.next=current;
            previous.next=newNode;
         }
         //节点插入成功,节点个数+1
         this.length+=1;
         return true;
         
     }


     //获取对应位置的元素
     LinkedList.prototype.get=function(position){
         //判断position合法,因为position是从0开始的,举个例子,有0 1 2 ,此时length=3,position不能大于等于3
         if(position<0||position>=this.length){
             return false;
         }
         //找位置
         var current=this.head;
         var index=0;//从位置0开始找
         //index最终到position前一个位置,current最终指向position位置的节点
         while(index++<position){
             current=current.next;
         }
         //找到后返回元素
         return current.data;
     }


     //返回元素在链表中的索引,如果链表中没有,返回-1
     LinkedList.prototype.indexOf=function(data){
         var current=this.head;
         var index=0//默认链表节点索引从0开始
         var c=0
         while(current){
             if(current.data==data){
                 return index;
             }
             else{
                current=current.next;
                index+=1;
             }
         }
         //匹配玩所有的节点,都没找到
         return -1;
     }


     //修改某个位置的元素
    LinkedList.prototype.update=function (position,data){
        //判断position合法,因为position是从0开始的,举个例子,有0 1 2 ,此时length=3,position不能大于等于3
        if(position<0||position>=this.length){
             return false;
         }
         //找位置
         var current=this.head;
         var index=0;//从位置0开始找
         //index最终到position前一个位置,current最终指向position位置的节点
         while(index++<position){
             current=current.next;
         }
         //找到后更改元素
         current.data=data;
         return true;
    }


     //从链表的特定位置删除一项
     LinkedList.prototype.removeAt=function(position){
         //判断position合法性
         if(position<0||position>=this.length){
             return false;
         }
         //如果删除的是第一个元素,让head直接指向它的next
         if(position==0){
             this.head=this.head.next;
         }
         //不是第一个元素时,找到要删除的位置的节点由current指向,要删除位置的前一个节点由previous指向,让previous.next指向current.next
         else{
             var current=this.head;
             var previous=null;
             var index=0;
             while(index++<position){
                 previous=current;
                 current=current.next;
             }
             previous.next=current.next;
         }
         //删除节点成功,节点数-1
         this.length-=1;
         return true;
     }


     //从链表中移除一项
     //通过indexOf()方法找到对应位置,然后根据位置删除对应项removeAt(position);
     LinkedList.prototype.remove=function(data){
         var position=this.indexOf(data);
         return this.removeAt(position);
     }


     //链表是否为空
    LinkedList.prototype.IsEmpty=function(){
        return this.length?false:true;
    }

    
    //返回链表的个数
    LinkedList.prototype.Size=function(){
        return this.length;
    }


    //将链表以字符串的形式输出 
    LinkedList.prototype.toString=function(){
        var str='';
        var current=this.head;
        while(current) {
            str+=current.data+' ';
            current=current.next;
        }
        return str;    
        }

   }


   
   //使用链表
   var linkedList=new LinkedList();

   linkedList.append('abc');
   linkedList.append('123');
   linkedList.append('hello');
   console.log(linkedList.toString());//abc 123 hello 
   linkedList.insert(0,'####');
   linkedList.insert(2,'?');
   console.log(linkedList.toString());//#### abc ? 123 hello 
   console.log(linkedList.get(1));//abc
   console.log(linkedList.indexOf('@'));//-1
   console.log(linkedList.indexOf('?'));//2
   console.log(linkedList.update(1,'@'));//true
   console.log(linkedList.toString());//#### @ ? 123 hello 
   console.log(linkedList.removeAt(1));//true
   console.log(linkedList.toString());//#### ? 123 hello 
   console.log(linkedList.remove('data'));//false
   console.log(linkedList.remove('123'));//true
   console.log(linkedList.toString());//#### ? hello 
   console.log(linkedList.IsEmpty());//false
   console.log(linkedList.Size());//3

结果:
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值