zoukankan      html  css  js  c++  java
  • 双向链表插入删除反序

    <!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>
    function ListNode(value){
    this.val = value
    this.pre = null
    this.next = null
    }
    class List {
    constructor(){
    this.HeadList = new ListNode('head')
    }
    find(item){
    let cur = this.HeadList
    while(cur.val != item){
    cur = cur.next
    }
    return cur
    }
    insert(node,item){
    let newnode = new ListNode(node)
    let cur = this.find(item)
    newnode.next = cur.next
    newnode.pre = cur
    cur.next = newnode
    if(newnode.next){
    newnode.next.pre = newnode
    }
     
    }
    display(){
    let cur = this.HeadList
    while(cur.next){
    console.log(cur.next.val)
    cur = cur.next
    }
    }
    remove(item){
    let cur = this.find(item)
    if(cur.next){
    cur.pre.next = cur.next
    cur.next.pre = cur.pre
    cur.next = null
    cur.pre = null
    }else{
    cur.pre.next = null
    cur.next = null
    }
    }
    findLast(){
    let cur = this.HeadList
    while(cur.next){
    cur = cur.next
    }
    return cur
    }
    rev(){
    let cur = this.findLast()
    while(cur.pre){
    console.log(cur.val)
    cur = cur.pre
    }
    }
    }
    let baic = new List()
    baic.insert('1','head')
    baic.insert('2','1')
    baic.insert('3','2')
    baic.insert('4','1')
    baic.display()
    console.log(baic,'^^__^^')
    baic.remove('4')
    baic.display()
    console.log('&&&&&')

    baic.rev()




    </script>
    </body>
    </html>
  • 相关阅读:
    RESTClient使用
    web.xml 的加载过程
    I18N、L10N、G11N
    Oracle游标示例
    MyEclipse转换Eclipse项目无法启动问题(转)
    eclipse默认文件编码
    JAVA为什么要配置环境变量,怎样配置
    TODO Auto-generated method stub
    vc6开发ActiveX并发布全攻略(三)(转)
    高效使用hibernate-validator校验框架
  • 原文地址:https://www.cnblogs.com/MDGE/p/14698552.html
Copyright © 2011-2022 走看看