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

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="UTF-8">
     5         <title>循环链表</title>
     6     </head>
     7     <body>
     8         <script>
     9             function Node(ele){
    10                 this.ele = ele;
    11                 this.next = null;
    12             }
    13             function linkList(){
    14                 this.head = new Node("head");
    15                 this.head.next = this.head;
    16                 this.find = find;
    17                 this.insert = insert;
    18                 this.display = display;
    19                 this.findPrev = findPrev;
    20                 this.remove = remove;
    21             }
    22             
    23             function display(){
    24                 var currNode = this.head;
    25                 while (currNode.next != null && currNode.next.ele != "head"){
    26                     console.log(currNode.next.ele);
    27                     currNode = currNode.next;
    28                 }
    29             }
    30             
    31             function find(item){
    32                 var currNode = this.head;
    33                 while(currNode.ele != item  && currNode.next.ele != "head" ){
    34                     currNode = currNode.next;
    35                 }
    36                 return currNode;
    37             }
    38             
    39             function insert(newEle,ele){
    40                 var newNode = new Node(newEle);
    41                 var current = this.find(ele);
    42                 newNode.next = current.next;
    43                 current.next = newNode;
    44             }
    45             
    46             function findPrev(item){
    47                 var currNode = this.head;
    48                 while ((currNode.next != null) && (currNode.next.ele != item)){
    49                     currNode = currNode.next;
    50                 }
    51                 return currNode;
    52             }
    53             
    54             function remove(item){
    55                 var prev = this.findPrev(item);
    56                 if(prev.next != null){
    57                     prev.next = prev.next.next;
    58                 }
    59             }
    60             
    61             
    62             
    63             var obj = new linkList();
    64             obj.insert("zhangsan","head");
    65             obj.insert("lisi","zhangsan");
    66             obj.insert("zhaowu","lisi");
    67             obj.insert("wangliu","zhaowu");
    68             obj.display();
    69             console.log(obj.findPrev("zhaowu"),"zhaowu");
    70             obj.remove("lisi");
    71             obj.display();
    72         </script>
    73     </body>
    74 </html>
  • 相关阅读:
    puppet运维配置实列
    puppet yum仓库
    autoSvn
    centos svn快速搭建
    无交互 直接传入 -yes
    puppet案例
    if
    CentosX64使用yum快速搭建xen虚拟化环境
    groupinfo
    Ubuntu编码问题
  • 原文地址:https://www.cnblogs.com/chengyunshen/p/7191895.html
Copyright © 2011-2022 走看看