zoukankan      html  css  js  c++  java
  • JavaScript数据结构-7.链表

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="UTF-8">
     5         <title></title>
     6     </head>
     7     <body>
     8         <script>
     9             
    10             function Node(ele){
    11                 this.ele = ele;
    12                 this.next = null;
    13             }
    14             
    15             function linkList(){
    16                 this.head = new Node("head");
    17                 this.find = find;
    18                 this.insert = insert;
    19                 this.remove = remove;
    20                 this.findPrev = findPrev;
    21                 this.display = display;
    22             }
    23             
    24             function find(item){
    25                 var currNode = this.head;
    26                 while(currNode.ele != item ){
    27                     currNode = currNode.next;
    28                 }
    29                 return currNode;
    30             }
    31             
    32             function insert(newEle,ele){
    33                 var newNode = new Node(newEle);
    34                 var current = this.find(ele);
    35                 newNode.next = current.next;
    36                 current.next = newNode;
    37             }
    38             
    39             function display(){
    40                 var currNode = this.head;
    41                 while(currNode.next != null){
    42                     console.log(currNode.next.ele);
    43                     currNode = currNode.next;
    44                 }
    45             }
    46             
    47             function findPrev(item){
    48                 var currNode = this.head;
    49                 while ((currNode.next != null) && (currNode.next.ele != item)){
    50                     currNode = currNode.next;
    51                 }
    52                 return currNode;
    53             }
    54             
    55             function remove(item){
    56                 var prev = this.findPrev(item);
    57                 if(prev.next != null){
    58                     prev.next = prev.next.next;
    59                 }
    60             }
    61             
    62             
    63             
    64             var obj = new linkList();
    65             obj.insert("zhangsan","head");
    66             obj.insert("lisi","zhangsan");
    67             obj.insert("zhaowu","lisi")
    68             console.log(obj.find("lisi"));
    69             obj.display();
    70             obj.remove("lisi");
    71             obj.display();
    72         </script>
    73     </body>
    74 </html>
  • 相关阅读:
    力学,结构动力NewMark法软组织模拟
    力学,非线性形变
    力学,线性形变
    波动方程水面模拟(简版)
    FFT海面(简版)
    测地极坐标参数化纹理贴图
    参数化离散指数映射纹理贴图
    Gridview中各个事件的操作以及FindControl
    CSS样式
    博客声明(博客大多均非原创文章,只用于记录)
  • 原文地址:https://www.cnblogs.com/chengyunshen/p/7191886.html
Copyright © 2011-2022 走看看