首页 > 其他分享 >双向链表-new

双向链表-new

时间:2024-04-03 13:44:37浏览次数:15  
标签:node current head index next 链表 双向 new element

<!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  DoubleNode extends Node {             constructor(element){                 super(element)                 this.prev = null             }         }
        class DoubleLinkedList extends LinkList {             constructor(){                 super()                 this.tail = null             }
            push(element){                 const node = new DoubleNode(element)                 if(this.head === null) {                     this.head = node                     this.tail = node                 } else {                     this.tail.next = node                     node.prev = this.tail                     this.tail = node                 }                 this.count++             }
            insert(element,index){                 if(index >=0 && index < this.count){                     const node = new DoubleNode(element)                     let current = this.head                     if(index === 0) {                         if(this.head === null){                             this.head = node                               this.tail = node                         } else {                             node.next = this.head                             this.head.prev = node                               this.head = node                           }                     } else if(index === this.count) {                         current = this.tail                         current.next = node                         node.prev = current                         this.tail = node                     } else {                         const previous = this.getNodeAt(index-1)                         current = previous.next
                        node.next = current                         current.prev = node                         previous.next = node                         node.prev = previous                     }                     this.count++                       return true                 }                 return false             }
            removeAt(index) {                 if(index >= 0 && index < this.count) {                     let current = this.head                     if(index === 0){                         this.head = this.head.next                         if(this.count === 1) {                             this.tail= null                         } else {                             this.head.prev = null                         }                     } else if(index === this.count-1){                         current = this.tail                         this.tail = current.prev                         this.tail.next = null                     } else {                         let previous = this.getNodeAt(index-1)                         current = previous.next                         previous.next = current.next                         currrent.next.prev = previous                     }
                    this.count--                     return current.element                 }                 return               }
            getHead(){                 return this.head             }
            getTail(){                 return this.tail             }
        }
        var list = new DoubleLinkedList()         list.push(100)         list.push(200)         list.push(300)         list.push(400)
        console.log(list)
    </script>     </body> </html>

标签:node,current,head,index,next,链表,双向,new,element
From: https://www.cnblogs.com/eric-share/p/18112505

相关文章

  • 集合set-new
    <!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <metahttp-equiv="X-UA-Compatible"content="IE=edge">  <metaname="viewport"content="width=d......
  • 【经典算法】LeetCode 21:合并两个有序链表Java/C/Python3实现含注释说明,Easy)
    合并两个有序链表题目描述思路及实现方式一:迭代(推荐)思路代码实现Java版本C语言版本Python3版本复杂度分析方式二:递归(不推荐)思路代码实现Java版本C语言版本Python3版本复杂度分析总结相似题目标签:字符串处理、前缀判断题目描述将两个升序链表合并为一个新的升......
  • SWUST OJ 952 单链表插入
    一、题目题目描述建立长度为n的单链表,在第i个结点之前插入数据元素data。输入第一行为自然数n,表示链式线性表的长度;第二行为n个自然数表示链式线性表各元素值;第三行为指定插入的位置i;第四行为待插入数据元素data。输出指定插入位置合法时候,输出插入元素后的链式线性......
  • 链表
    template<typenameT>classthreadsafe_list{structnode//1{std::mutexm;std::shared_ptr<T>data;std::unique_ptr<node>next;node()://2next(){}node(Tconst&value)://3data(std......
  • [LeetCode]12. K 个一组翻转链表 C语言实现
    Problem:25.K个一组翻转链表目录思路解题方法复杂度Code思路官方思路多指针+翻转链表+结构体解题方法定义多指针用来查找的头节点每一组的头节点每一组的尾节点,用来找到下一组头节点复杂度时间复杂度:添加时间复杂度,示例:$O(n)$空间复杂度:添加空......
  • 今早,这个中国人做的开源项目被YC的hacker news收录了!
    众所周知,YC在创业者心中的地位,它孕育出了openai、airbnb等众多硅谷知名企业。就在今早,YC的hackernews把这个由中国人做的开源项目收录了!这是一个什么项目?先上开源地址:https://github.com/saasfly/saasfly简介中可以看出,这是一个具备足够创新的“下一代SaaS模版”,旨在帮助使......
  • 【leetcode】链表篇刷题 --
    //删除指定value节点classSolution{public:ListNode*removeElements(ListNode*head,intval){//单独处理headwhile(head!=NULL&&head->val==val){ListNode*temp=head;head=head->next;......
  • C语言链表:链式魔法,数据之美
    导入链表,作为C语言中一种基础且重要的数据结构,以其独特的方式组织和存储数据,成为了解决许多复杂问题的关键。下面,我们将更具体地探讨C语言链表的各个方面。一、链表的基本结构链表由一系列节点组成,每个节点通常包含两部分:数据域和指针域。数据域用于存储实际的数据,而指针域......
  • 双向长短期BiLSTM的回归预测-附MATLAB代码
    BiLSTM是一种带有正反向连接的长短期记忆网络(LSTM)。BiLSTM通过两个独立的LSTM层,一个按时间顺序处理输入,另一个按时间倒序处理输入,分别从正向和反向两个方向捕捉输入序列的特征。具体地,正向LSTM按时间步从左到右处理输入序列,每个时间步的隐藏状态。预测结果如下:代码获取方......
  • NewStarCTF-firstweek
    一、Crypto-brainfuck1.附件内容如下。++++++++[>>++>++++>++++++>++++++++>++++++++++>++++++++++++>++++++++++++++>++++++++++++++++>++++++++++++++++++>++++++++++++++++++++>++++++++++++++++++++++>++++++++++++++++++++++++>+++++......