javascript数据结构和算法——链表,双向链表

javascript数据结构和算法——链表,双向链表

一、单向链表
<script>
class Node {
  constructor(element) {
    this.element = element;
    this.next = null;
  }
}

class linkList {
  constructor() {
    this.head = null;
    this.length = 0;
  }
  // 向链表中添加元素
  append(element) {
    let newEle = new Node(element)
    if (this.length === 0) {
      this.head = newEle;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }
      current.next = newEle;
    }
    this.length++;
  }
  // 输出字符串的方法
  toString() {
    let result = '';
    let current = this.head;
    while (current) {
      result += current.element + " ";
      current = current.next;
    }
    return result;
  }
  // 插入链表算法
  insert(position, element) {
    let insertEle = new Node(element);
    if (position < 0 || position > this.length) {
      return false;
    }
    // 当插入的位置为0时
    if (position == 0) {
      insertEle.next = this.head;
      this.head = insertEle;
    } else {
      let index = 0;
      let current = this.head;
      let prov = null;
      while (index++ < position) {
        prov = current;
        current = current.next;
      }
      prov.next = insertEle;
      insertEle.next = current;
    }
    this.length++;
    return true;
  }
  // 获取某一个位置的元素数据
  get(position) {
    if (position < 0 || position > this.length) {
      return false;
    }
    let index = 0;
    let current = this.head;
    while (index++ < position) {
      current = current.next;
    }
    return current.element;
  }
  // indexOf()用来查找
  indexOf(ele) {
    let index = 0;
    let current = this.head;
    while (current) {
      if (current.element === ele) {
        return index;
      }
      current = current.next;
      index++;
    }
    return -1;
  }
  // updata()方法来更新数据
  updata(position, newData) {
    if (position < 0 || position > this.length) {
      return false;
    }
    let current = this.head;
    let index = 0;
    while (index++ < position) {
      current = current.next;
    }
    current.element = newData;
    return true;
  }
  removeAt(position) {
    if (position < 0 && position >= this.length) {
      return false;
    } else {
      let index = 0;
      let current = this.head;
      let prov = null;
      while (index++ < position) {
        prov = current;
        current = current.next;
      }
      prov.next = current.next;
    }
    return true;
  }
  // 删除某一个元素
  remove(ele) {
    let position = this.indexOf(ele);
    this.removeAt(position);
  }
  // 判断元素是否为空
  isEmpty(){
    return this.length == 0;
  }
  // 返回链表最终的长度
  size(){
    return this.length;
  }
}
let list = new linkList();
list.append("aaa");
list.append("bbb");
list.append("ccc");
list.append('ddd');
console.log(list.toString());
console.log(list.isEmpty());
console.log(list.size());
</script>
二、双向链表
<script>
class Node {
  constructor(element) {
    this.element = element;
    this.next = null;
    this.prov = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
    this.length = 0;
  }
  // 这里append()方法就是对其进行添加元素
  append(element) {
    let newNode = new Node(element);
    if (this.length === 0) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      newNode.prov = this.tail;
      this.tail.next = newNode;
      this.tail = newNode;
    }
    this.length++;
  }
  // 将链表转化成字符串
  // 1、toString()
  toString() {
    let result = "";
    let current = this.head;
    while (current) {
      result += current.element + " ";
      current = current.next;
    }
    return result;
  }
  // 2、forwordString()
  forwordString() {
    let result = "";
    let current = this.tail;
    while (current) {
      result += current.element + " ";
      current = current.prov;
    }
    return result;
  }
  // 3、backWordString()
  backWordString() {
    return this.toString();
  }
  // insert(position,element)插入
  insert(position, element) {
    let insertNode = new Node(element);
    // 确定位置符合条件
    if (position < 0 && position > this.length) {
      return false;
    }
    // 判断长度是否为0
    if (this.length === 0) {
      this.head = insertNode;
      this.tail = insertNode;
    } else {
      // 当长度不等于0
      if (position === 0) {
        insertNode.next = this.head;
        this.head.prov = insertNode;
        this.head = insertNode;
      } else if (position === this.length) {
        insertNode.prov = this.tail;
        this.tail.next = insertNode;
        this.tail = insertNode;
      } else {
        let index = 0;
        let current = this.head;
        while (index++ < position) {
          current = current.next;
        }
        insertNode.next = current;
        insertNode.prov = current.prov;
        current.prov.next = insertNode;
        current.prov = insertNode;
      }
    }
    this.length++;
    return true;
  }
  // get(position)获取元素数据
  get(position) {
    if (position < 0 && position >= this.length) {
      return false;
    }
    // 双向链表和单向链表之间的区别
    let index
    let current;
    if (this.length / 2 > position) {
      // 从后向前数
      index = this.length - 1;
      current = this.tail;
      while (index-- > position) {
        current = current.prov;
      }
    } else {
      // 从前向后数
      index = 0;
      current = this.head;
      while (index++ < position) {
        current = current.next;
      }
    }
    return current.element;
  }
  //  查找元素下标
  indexOf(element) {
    let index = 0;
    let current = this.head;
    while (current) {
      if (current.element === element) {
        return index;
      }
      current = current.next;
      index++;
    }
    return -1;
  }
  // 更新数据
  updata(position, element) {
    if (position < 0 && position >= this.length) {
      return false;
    }
    let index;
    let current;
    // 双向链表和单向链表的区别
    if (this.length / 2 > position) {
      // 在前半部分
      index = 0;
      current = this.head;
      while (index++ < position) {
        current = current.next;
      }
    } else {
      // 在后半部分
      index = this.length - 1;
      current = this.tail;
      while (index-- > position) {
        current = current.prov;
      }
    }
    current.element = element;
    return true;
  }
  // removeAt(position)//删除一个元素
  removeAt(position) {
    if (position < 0 && position >= this.length) {
      return false;
    }
    let index = 0;
    let current = this.head;
    if (this.length === 1) {
      this.head = null;
      this.tail = null;
    } else {
      if (position === 0) {
        this.head = this.head.next;
        this.head.prov = null;
      } else if (position === this.length - 1) {
        current = this.tail;
        this.tail = this.tail.prov;
        this.tail.next = null;
      } else {
        while (index++ < position) {
          current = current.next;
        }
        current.prov.next = current.next;
        current.next.prov = current.prov;
      }
    }
    this.length--;
    return current.element;
  }
  // remove(ele)删除指定元素
  remove(ele){
    let place = this.indexOf(ele);
    this.removeAt(place);
    this.length--;
  }
  // 判断链表是否为空
  isEmpty(){
    return this.length=== 0;
  }
  // 链表的长度
  size(){
    return this.length;
  }
  // 取出第一个元素
  forHead(){
    return this.head.element;
  }
  // 取出最后一个元素
  forBack(){
    return this.tail.element;
  }
}
let double = new DoublyLinkedList();
double.append("aaa");
double.append("bbb");
double.append("ccc");
double.append("ddd");
console.log(double.isEmpty());
console.log(double.size());
console.log(double.forHead());
console.log(double.forBack());
</script>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值