链表

链表中的元素在内存中不必是连续的空间
链表的每个元素由一个存储元素本身的节点和一个指向下个元素的引用组成

链表的常见操作
append(element):向链表的尾部添加新的元素
insert(positon,element):向链表的特定位置添加元素
get(position):获取对应为位置的元素
indexOf(element):返回元素在链表中的索引,没有则返回-1
update(positon,element):修改某个位置的元素
removeAt(positon):从链表的特定位置移除一项
remove(element):从链表中移除一项
isEmpty()
size()
toString()

//单链表
function LinkedList(){
    function Node(data){
       this.data = data;
       this.next = null;
    }
    this.head = null;
    this.length = 0;
    
    LinkedList.prorotype.append() = function(data){
        var newNode = new Node(data); 
        if(this.length == 0){
           this.head = newNode;
        }else{
           var current = this.head;
           while(current.next){
               current = current.next;
           }
           current.next = newNode; 
        }
       this.length +=1;
    }
     
    LinkedList.prorotype.toString() = function(){
        var current = this.head;
        var listString = "";
        while(current){
               listString += current.data + "" ;
               current = current.next; 
         }
         return listString;
   }
    LinkedList.prorotype.insert() = function(positon,data){
        if(positon < 0 || positon > this.length){
            return  false;
        }
        var newNode = new Node(data);
        if(position == 0){
            newNode.next = this.head;
            this.head = newNode;
        
        }else{
           var index = 0;
           var current = this.head;
           var previous = null;
           while(index ++ <position){
               previous = current;
               current = current.next; 
           }
           newNode.next = current;
           previous.next = newNode;
        }
        this.length +=1return  true;
    }
    
     LinkedList.prorotype.get() = function(positon){
         if(positon < 0 ||positon >= this.length){
             return null;
         }
         var current = this.head;
         var index = 0;
         while(index ++ < positon){
             current = current.next;
         }
     }
     
    LinkedList.prorotype.indexOf() = function(data){
        var current = this.head;
        var index = 0;
        while(current){
           if(current.data == data){
               return index;
           }
           current = current.next;
           index +=1;
        }
        return -1;
    } 

    LinkedList.prorotype.update() = function(position,newdata){
         if(positon < 0 ||positon >= this.length){
             return false;
         }
         var current = this.head;
         var index = 0;
         while(index ++ < positon){
              current = current.next;
         }
         current.data = newdata;
         return true;
    }


    LinkedList.prorotype.removeAt() = function(position){
        if(positon < 0 ||positon >= this.length){
             return null;
        }
        if(position == 0){
             this.head = this.head.next;
        }else{
             var index = 0;
             var current = this.head;
             var previous = null;
             while(index ++ <position){ 
                previous = current;
                current = current.next;
             }
             previous.next = current.next;
        }
        this.length -=1;
        return current.data;
    }

    
   LinkedList.prorotype.remove() = function(data){
        var  positon = linkedList.indexOf(data)
        return this.removeAt(position);
   }

   LinkedList.prorotype.isEmpty() = function(data){
        return this.length ==0;
   }
   LinkedList.prorotype.size() = function(data){
        return this.length;
   }  
    
     
       
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值