zoukankan      html  css  js  c++  java
  • js链表操作

     1 <!doctype html>
     2 <html lang="en">
     3  <head>
     4   <meta charset="UTF-8">
     5   <title>Document</title>
     6   <script>
     7     function Node(v){
     8         this.value=v;
     9         this.next=null;
    10     }
    11     function ArrayList(){
    12         this.head=new Node(null);
    13         this.tail = this.head;
    14         this.append=function(v){
    15             node = new Node(v);
    16             this.tail.next=node;
    17             this.tail=node;
    18         }
    19         this.insertAt=function(ii,v){
    20             node = new Node(v);
    21             //找到位置的节点
    22             tempNode=this.head;
    23             for(i=0;i<ii;i++){
    24                 if(tempNode.next!=null){
    25                     tempNode=tempNode.next;
    26                 }else{
    27                     break;
    28                 }
    29             }
    30             node.next=tempNode.next;
    31             tempNode.next = node;
    32         }
    33         this.removeAt=function(ii){
    34             node1=this.head; //要删除节点的前一个节点
    35             for(i=0;i<ii;i++){
    36                 if(node1.next!=null){
    37                     node1=node1.next;
    38                 }else{
    39                     break;
    40                 }
    41             }
    42             node2=node1.next;    //要删除的节点
    43             if(node2!=null){
    44                 node1.next = node2.next;
    45                 if(node2.next==null){
    46                     this.tail=node1;
    47                 }
    48             }
    49         }
    50         
    51     }
    52     function Iterator(arryList){
    53         this.point=arryList.head;
    54         this.hasNext=function(){
    55             if(this.point.next!=null){
    56                 this.point=this.point.next;
    57                 return true;
    58             }else{
    59                 return false;
    60             }
    61         }
    62         this.next=function(){
    63             return this.point.value;
    64         }
    65     }
    66     
    67     var arry = new ArrayList();
    68     arry.append(1);
    69     arry.append(2);
    70     arry.append(3);
    71     arry.insertAt(1,8);
    72     arry.insertAt(0,9);
    73     arry.insertAt(100,100);
    74     arry.insertAt(1000,1000);
    75     arry.insertAt(1,200);
    76     arry.insertAt(200,2000);
    77     
    78     iterator = new Iterator(arry);
    79     while(iterator.hasNext()){
    80         document.write(iterator.next());
    81         document.write('<br/>');
    82     }
    83   </script>
    84  </head>
    85  <body>
    86   
    87  </body>
    88 </html>
  • 相关阅读:
    2014/4/9-最近所想
    DNS坑爹呢?!
    c#委托使用
    sqlserver 获取一个月有多少天
    定时提醒模型
    sqlserver导入dbf文件
    json对象中根据主键判断是否有重复数据
    差异备份与恢复
    html编码和解码
    opendatasource问题
  • 原文地址:https://www.cnblogs.com/yu-hailong/p/7445750.html
Copyright © 2011-2022 走看看