首页 > 其他分享 >循环列表-new

循环列表-new

时间:2024-04-03 13:45:26浏览次数:15  
标签:count index head next current 循环 element new 列表

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>Document</title> </head> <body>
    <script>
        class Node{             constructor(element){                 this.element = element                 this.next = null               }         }
        class LinkList {                         constructor(){                 this.count = 0                 this.head = null             }
                        push(element){                 const node = new Node(element)                               if(this.head === null) {                     this.head = node                   }else {                     let current = this.head                     while(current.next != null){                         current = current.next                       }                     current.next = node                 }                 this.count++             }
            // 指定位置删除             removeAt(index){
                if(index >= 0 && index < this.count){                     let current = this.head                     if(index === 0)  {                         this.head = this.head.next                     } else {                        let previous                           for(let i = 0; i < index; i++){                                 previous = current                             current = current.next                         }                         previous.next = current.next                     }                     this.count--                     return current.element                 }             }
            getNodeAt(index) {                 if(index >= 0 && index < this.count){                     let node = this.head                     for(let i = 0; i <index;i++){                         node = node.next                     }                     return node                 }                 return             }
            // 指定位置删除             removeAt2(index){
                if(index >= 0 && index < this.count){                     let current = this.head                     if(index === 0)  {                         this.head = this.head.next                     } else {                         let previous = this.getNodeAt(index - 1)                         current = previous.next                         previous.next = current.next                     }                     this.count--                     return current.element                 }             }
            equalFn(a,b){                 // return a === b                 return JSON.stringify(a) === JSON.stringify(b)                 // imumutable 判断两个元素是否相等             }
            //  返回指定数据索引             indexOf(element){                 let current = this.head                 for(let i = 0; i < this.count;i++){                     if(this.equalFn(element,current.element)){                         return i                     }                     current = current.next                 }                 return -1             }
            // 指定数据删除             remove(element){                 const index = this.indexOf(element)                 return this.removeAt(index)             }               //指定位置插入一个元素             insert(element,index){                               if(index >= 0 && index <= this.count){                     const node = new Node(element)                     if(index === 0){                         const current = this.head                         node.next = current                         this.head = node                     } else{                         // 找到指定位置数据  和 上级的数据                         let previous = this.getNodeAt(index - 1)                         const current = previous.next                         node.next = current                         prevent.next = node                     }                     this.count++                     return  true                  }                  return false
            }         }             </script>
    <script>           class CirularLinkedList extends LinkList {             constructor(){                 super()             }                         push(element){                 const node = new Node(element)                 let current                 if(this.head === null){                     this.head = node                 }else {                     current = this.getNodeAt(this.count - 1)                     current.next = node                 }                 node.next = this.head                 this.count++             }
            insert(element,index){                 if(index >=0 && index <= this.count) {                     const node = new Node(element)                     let current = this.head                     if(index === 0){                         if(this.head === null) {                             this.head = node                             node.next = this.head                         } else {                             node.next = current                             current = this.getNodeAt(this.count - 1 )                             this.head = node                             current.next = this.head                         }                     } else {                         const previous = this.getNodeAt(index - 1)                         node.next = previous.next                         previous.next = node                     }
                    this.count++                     return true                 }
                return false             }  
            removeAt(index){                 if(index >=0 && index < this.count) {                     let current = this.head                     if(index === 0){                         if(this.count === 1){                             this.head = undefined                         } else {                             let last = this.getNodeAt(this.count - 1)                             this.head = this.head.next                             last.next = this.head                         }                     } else {                         const previous = this.getNodeAt(index - 1)                         current = previous.next                         previous.next = current.next                     }
                    this.count--                     return current.element                 }                 return             }         }
        var list = new CirularLinkedList()         list.push(100)         list.push(200)         list.push(300)         list.push(400)
        console.log(list)
    </script>     </body> </html>

标签:count,index,head,next,current,循环,element,new,列表
From: https://www.cnblogs.com/eric-share/p/18112509

相关文章

  • 双向链表-new
    <!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <metahttp-equiv="X-UA-Compatible"content="IE=edge">  <metaname="viewport"content="width=d......
  • 集合set-new
    <!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <metahttp-equiv="X-UA-Compatible"content="IE=edge">  <metaname="viewport"content="width=d......
  • 数学建模-------MATLAB分支循环&&断点调试
    1.if语句(1)分段函数的引入(这里的数据表示的是分数的不同区间对应的等级)(1)这个就是一个十分简单的if语句,无论是if还是elseif后面都是不能添加任何分号的,这个例子就是一个分段的函数,在不同的区间赋予不同的等级,这里划分了1,2,3,4一共4个等级,只有前面的三个全部都不满足的时候,才......
  • 理解列表的引用和浅拷贝,体会path和path[:]的不同
    文章目录零、从哪里来一、先看算法题(回溯法)二、扩展三、总结零、从哪里来引用与浅拷贝与深拷贝记得看这篇文章总结最后,不亏,没看完的话,你会后悔一辈子。一、先看算法题(回溯法)给你一个整数数组nums,其中可能包含重复元素,请你返回该数组所有可能的子集。解集不能......
  • 顺序结构、选择结构、循环结构
    一、顺序结构二、选择结构importjava.util.Scanner;publicclassifdemo{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.println("请输入内容:");Strings=scanner.nextLine();......
  • 就业班 第二阶段 2401--3.28 day8 shell之循环控制
    七、shell编程-循环结构shell循环-for语句foriin{取值范围}  #for关键字i变量名in关键字取值范围格式12345do          #do循环体的开始循环体done         #done循环体的结束#!/usr/......
  • 列表和字典的内置方法
    1列表的内置方法gf_name_list=['高圆圆','刘亦菲','赵丽颖','范冰冰','李嘉欣']一、增(1)列表最后位置追加一个值gf_name_list.append("橘梨纱")(2)向列表任意位置插入一个值gf_name_list.insert(1,"橘梨纱")(3)扩展列表gf_name_list.extend([&......
  • 07字符串内置方法_列表的内置方法_列表操作_字典的内置方法
    【一】字符串内置方法【1】find方法name="dreamHello"#默认是从左向右找并且返回当前字符在字符串内部的索引坐标print(name.find('d'))#0#默认只能找一次,找到了就不会再找下去了print(name.find('e'))#2#可以指定寻找的区间print(name.find('e',5,-1))#7......
  • Python笔记----列表(List)【附代码】
    1.列表介绍   列表既是Python中最基本的数据结构又是最常用的数据类型   创造列表很简单,只要把数据用中括号括起来,数据之间用逗号隔开就可以了。2.列表的创建   列表的数据项不需要具有相同的类型,不同数据类型都可以装,可以存储的信息非常丰富3.列表......
  • 22_shell脚本条件判断、函数和循环
    shell脚本条件判断、函数和循环一、shell脚本条件判断​shell脚本支持条件判断,虽然可以通过&&和||来实现简单的条件判断,但是稍微复杂一点的场景就不适合了。shell脚本提供了ifthen条件判断语句,写法if条件判断;then//判断成立要做的事情fi还有ifthenelse语句,写法......