• 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>
  • 相关阅读:
    jquery两个滚动条样式
    js双层动画幻灯
    漂浮QQ
    js物理弹性窗口
    js抽奖跑马灯程序
    经典算法
    判断手机浏览器终端设备
    javascript判断手机旋转横屏竖屏
    【转】处理百万级以上的数据提高查询速度的方法
    Linux -- Centos 下配置LNAMP 服务器环境
  • 原文地址:https://www.cnblogs.com/chengyunshen/p/7191895.html
Copyright © 2020-2023  润新知