当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js链表操作(实例讲解)

js链表操作(实例讲解)

2017年12月12日  | 移动技术网IT编程  | 我要评论

如下所示:

<!doctype html>
<html lang="en">
 <head>
 <meta charset="utf-8">
 <title>document</title>
 <script>
  function node(v){
    this.value=v;
    this.next=null;
  }
  function arraylist(){
    this.head=new node(null);
    this.tail = this.head;
    this.append=function(v){
      node = new node(v);
      this.tail.next=node;
      this.tail=node;
    }
    this.insertat=function(ii,v){
      node = new node(v);
      //找到位置的节点
      tempnode=this.head;
      for(i=0;i<ii;i++){
        if(tempnode.next!=null){
          tempnode=tempnode.next;
        }else{
          break;
        }
      }
      node.next=tempnode.next;
      tempnode.next = node;
    }
    this.removeat=function(ii){
      node1=this.head; //要删除节点的前一个节点
      for(i=0;i<ii;i++){
        if(node1.next!=null){
          node1=node1.next;
        }else{
          break;
        }
      }
      node2=node1.next;  //要删除的节点
      if(node2!=null){
        node1.next = node2.next;
        if(node2.next==null){
          this.tail=node1;
        }
      }
    }
    
  }
  function iterator(arrylist){
    this.point=arrylist.head;
    this.hasnext=function(){
      if(this.point.next!=null){
        this.point=this.point.next;
        return true;
      }else{
        return false;
      }
    }
    this.next=function(){
      return this.point.value;
    }
  }
  
  var arry = new arraylist();
  arry.append(1);
  arry.append(2);
  arry.append(3);
  arry.insertat(1,8);
  arry.insertat(0,9);
  arry.insertat(100,100);
  arry.insertat(1000,1000);
  arry.insertat(1,200);
  arry.insertat(200,2000);
  
  iterator = new iterator(arry);
  while(iterator.hasnext()){
    document.write(iterator.next());
    document.write('<br/>');
  }
 </script>
 </head>
 <body>
 
 </body>
</html>

以上这篇js链表操作(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网