javascript plastikaweb链接列表数据结构 - https://repl.it/@plastikaweb/LightblueTomatoRaven

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript plastikaweb链接列表数据结构 - https://repl.it/@plastikaweb/LightblueTomatoRaven相关的知识,希望对你有一定的参考价值。

const es6 = require('es6');
function LinkedList() {
  this.head = null;
  this.tail = null;
}

LinkedList.prototype.addToHead = value => {
  const newNode = new Node(value, this.head, null);
  if (this.head) {
    this.head.prev = newNode;
  } else {
    this.tail = newNode;
  }
  this.head = newNode;
};

LinkedList.prototype.addToTail = value => {
  const newNode = new Node(value, null, this.tail);
  if (this.tail) {
    this.tail.next = newNode;
  } else {
    this.head = newNode;
  }
  this.tail = newNode;
};

LinkedList.prototype.removeHead = () => {
  if (!this.head) return null;
  const val = this.head.value;
  this.head = this.head.next;
  if (this.head) {
    this.head.prev = null;
  } else {
    this.tail = null;
  }
  return val;
};

LinkedList.prototype.removeTail = () => {
  if (!this.tail) return null;
  const val = this.tail.value;
  this.tail = this.tail.prev;
  if (this.tail) {
    this.tail.next = null;
  } else {
    this.head = null;
  }
  return val;
};

LinkedList.prototype.search = (searchValue) => {
  let currentNode = this.head;
  while(currentNode) {
    if (currentNode.value === searchValue) {
      return currentNode.value;
    }
    currentNode = currentNode.next;
  }
  return null;
}

LinkedList.prototype.indexOf = (value) => {
  let currentNode = this.head;
  const indexes = [];
  let index = 0;
  while(currentNode) {
    if (currentNode.value === value) {
      indexes.push(index);
    }
    currentNode = currentNode.next;
    index++;
  }
  return indexes;
};

function Node(value, next, prev) {
  this.value = value;
  this.next = next;
  this.prev = prev;
}

const LL = new LinkedList();

LL.addToHead('one');
LL.addToHead('two');
LL.addToHead('three');
LL.addToHead('two');

console.log(LL.indexOf('two'));

以上是关于javascript plastikaweb链接列表数据结构 - https://repl.it/@plastikaweb/LightblueTomatoRaven的主要内容,如果未能解决你的问题,请参考以下文章

javascript LightblueTomatoRaven由plastikaweb创建 - https://repl.it/@plastikaweb/LightblueTomatoRaven

javascript 由plastikaweb创建的hashTable - https://repl.it/@plastikaweb/hashTable

javascript 数据结构链表 - addToHead()addToTail() - https://repl.it/@plastikaweb/LightblueTomatoRaven

javascript 具有报告列链接的Javascript SET页面项目(自定义DA)

PHP / JQUERY - 为列数据创建超链接

JavaScript Hashmap散列算法