关于javascript:javascript-数据结构系列-一-单向链表

25次阅读

共计 2488 个字符,预计需要花费 7 分钟才能阅读完成。

javascript 数据结构单向链表 LinkedList
首先,实现 LinkedListNode,代码如下

export default class LinkedListNode {constructor(value, next = null) {
    this.value = value;
    this.next = next;
  }

  toString(callback) {return callback ? callback(this.value) : `${this.value}`;
  }
}

linklist 类实现

构造函数

  /**
   * @param {Function} [comparatorFunction]
   */
  constructor(comparatorFunction) {
    /** @var LinkedListNode */
    this.head = null;

    /** @var LinkedListNode */
    this.tail = null;

    this.compare = new Comparator(comparatorFunction);
  }

Linklist append 办法

  /**
   * @param {*} value
   * @return {LinkedList}
   */
  append(value) {const newNode = new LinkedListNode(value);

    // If there is no head yet let's make new node a head.
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;

      return this;
    }

    // Attach new node to the end of linked list.
    this.tail.next = newNode;
    this.tail = newNode;

    return this;
  }

delete 办法

  /**
   * @param {*} value
   * @return {LinkedListNode}
   */
  delete(value) {if (!this.head) {return null;}

    let deletedNode = null;

    // If the head must be deleted then make next node that is differ
    // from the head to be a new head.
    while (this.head && this.compare.equal(this.head.value, value)) {
      deletedNode = this.head;
      this.head = this.head.next;
    }

    let currentNode = this.head;

    if (currentNode !== null) {
      // If next node must be deleted then make next node to be a next next one.
      while (currentNode.next) {if (this.compare.equal(currentNode.next.value, value)) {
          deletedNode = currentNode.next;
          currentNode.next = currentNode.next.next;
        } else {currentNode = currentNode.next;}
      }
    }

    // Check if tail must be deleted.
    if (this.compare.equal(this.tail.value, value)) {this.tail = currentNode;}

    return deletedNode;
  }

find 办法

  /**
   * @param {Object} findParams
   * @param {*} findParams.value
   * @param {function} [findParams.callback]
   * @return {LinkedListNode}
   */
  find({value = undefined, callback = undefined}) {if (!this.head) {return null;}

    let currentNode = this.head;

    while (currentNode) {
      // If callback is specified then try to find node by callback.
      if (callback && callback(currentNode.value)) {return currentNode;}

      // If value is specified then try to compare by value..
      if (value !== undefined && this.compare.equal(currentNode.value, value)) {return currentNode;}

      currentNode = currentNode.next;
    }

    return null;
  }

reverse 办法

  /**
   * Reverse a linked list.
   * @returns {LinkedList}
   */
  reverse() {
    let currNode = this.head;
    let prevNode = null;
    let nextNode = null;

    while (currNode) {
      // Store next node.
      nextNode = currNode.next;

      // Change next node of the current node so it would link to previous node.
      currNode.next = prevNode;

      // Move prevNode and currNode nodes one step forward.
      prevNode = currNode;
      currNode = nextNode;
    }

    // Reset head and tail.
    this.tail = this.head;
    this.head = prevNode;

    return this;
  }

总结,以上代码实现 javascript 单向链表,代码蕴含两局部
LinkedListNode,LinkedList
其中 linkedList 包含如下办法
append,delete,find,reverse 等办法
如果对你有帮忙请点赞!

正文完
 0