[精华]javascript-数据结构-链表

[精华]javascript-模拟数据结构-链表

//链表的数据结构
class Node{
  //节点类
  constructor(element){
  this.element=element;
  this.next=null;
  }
}
function equals(a,b){
  return a===b;
  //对比两个元素是否相等
}
class LinkList{
  constructor(){
  this.count=0;
  //用来存储链表的长度
  this.equals=equals;
  //用来定义对比两个元素的方法
  this.head=null;
  //链表的头部,初始化值是null
  }
  push(element){
  //向链表中添加元素
    const node=new Node(element);
    if(this.head==null){
      //链表的头部是null的时候
      this.head=node
    }else{
      let current=this.head;
      //中间变量
      while(current.next!=null){
      current=current.next
      }
      current.next=node;
    } 
    this.count++;
    //链表插入元素,长度+1
  }
  getElementAt(index){
  //获取链表特定位置的元素
    if(index>=0&&index<this.count){
      let current=this.head
      for(let i=0;i<index&&this.head!=null;i++){
        current=current.next;
      } 
      return this.head 
    }else{
    return undefined;
    }
  }
  removeAt(index){
  //移除链表特定位置的元素
    if(index>=0&&index<this.count){
      let current=this.head;
      //用中间变量存储下链表
      if(index===0){
       this.head=current.next; 
      //链表的头等于上个中间变量next指向的值,链表头和next中间的值就被链表删除了
      }else{
       let previous=this.getElementAt(index-1) ;
        //要删除元素的链表中的上一个位置的元素
        //那么previous的next元素就是要删除的元素
        current=previous.next;//要删除的元素
        previous.next=current.next;
        //previous的next的就是要删除的元素的下一个元素
        //此时previous和删除元素下个元素中间要被删除的元素的值就被删除掉了-->看注释比较绕,看图直接就能明白
      } 
      this.count--;
      //链表的长度-1
    }else{
    return false
    }
  }
  insertAt(index,element){
    if(index>=0&&index<this.count){
      let current=this.head;
      if(index===0){
        this.head=current.next;
        current=element;
      }else{
      let previous=this.getElementAt(index-1)
      const node=new Node(element); 
      //创建符合链表的节点
      node.next=previous.next;
      //要插入的元素的下个指针直接指向了下个元素
      previous.next=node 
      //这里的元素的下个指针指向要插入的元素
      }
      this.count++

    }else{
      return false
    }
  }
  remove(element){
  //移除元素-->根据元素移除 
   let index=this.indexOf(element);
    if(index){
     let previous=this.getElementAt(index)
      let current=this.getElementAt(index-1)
      current.next=previous.next;
    }else{
      return false;
    }
    this.count--;
  }
  indexOf(element){
  //查找元素所在的位置
  let current=this.head;
    for(let i=0;i<this.count&&current!=null;i++){
      if(this.equals(element,current.element)){
      return i;
      }
      current=current.next;
    }
    return -1
  }
  size(){
  return this.count;
  }
  isEmpty(){
  return this.size()==0;
  }
}
const a=new LinkList()
a.push('hello',21)
a.push('world',3)
a.push(1,5)
console.log(a.getElementAt(1))
a.removeAt(1)
a.push(3,'你好')
a.push(1,'houhou')
console.log(a)
console.log(a.size())
console.log(a.isEmpty())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值