// Implement a single linked list
function Node(val, next) {
  this.val = (val === undefined ? 0 : val)
  this.next = (next === undefined ? null : next)
}

function ListNode() {
  let head = null
  let length = 0
  // Add elements
  this.add = function (val) {
    let newNode = new Node(val)
    let current = null
    if(! head) { head = newNode }else {
      // current = head
      // while (current.next) {
      // current = current.next
      // }
      current = this.findByNode(length - 1)
      current.next = newNode
    }

    length++
    return true
  }
  this.edit = function (val, newVal) {
    let current = this.find(val)
    current.val = newVal
    return newVal
  }
  // Insert the node
  this.insert = function (val, point) {
    let newNode = new Node(val)
    // Query the precursor node where the node is to be inserted
    // let index = 0
    // let prev = null
    // let current = head;
    if (point > -1 && point <= length) {
      if (point === 0) {
        newNode.next = head
        head = newNode
      } else {
        let current = this.findPreByNode(point)
        newNode.next = current && current.next ? current.next : null;
        current.next = newNode
        // while (index++ < point) {
        // prev = current;
        // current = current.next;
        // }
        // newNode.next = current;
        // prev.next = newNode;
      }
      length++
      return true
    } else {
      throw Error("Out of node range!")}}// Query the element subscript
  this.indexOf = function (val) {
    let current = head, index = -1
    while (current) {
      index++
      if (current.val === val) {
        return index
      }
      current = current.next
    }
    return -1
  }
  // Query the node by node element
  this.find = function (val) {
    let current = head
    while(current && current.val ! == val) { current = current.next// If the node does not exist current === null exits the loop
    }
    return current
  }
  // Query the node by node location
  this.findByNode = function (point) {
    let current = head
    let i = 0
    while (current && point > -1 && i++ < point) {
      current = current.next
    }
    return current
  }
  // Query the precursor node of a node
  this.findPre = function (val) {
    let current = head
    while(current && ! (current.next ! = =null && current.next.val === val)) {
      current = current.next
      // If there is no precursor node current === null jumps out of the loop
    }
    return current
  }
  // Query the precursor node of a node (by node location)
  this.findPreByNode = function (point) {

    if (point > -1 && point <= length) {
      let current = head
      let i = 0
      if (point === 0) {
        return null
      } else {
        while (++i < point) {
          current = current.next
        }
        return current
      }
    } else {
      return null}}// Delete elements (by element)
  this.delete = function (val) {
    let current = this.findPre(val)
    if (current) {
      current.next = current.next.next
      length--
      return val
    } else {
      return null}}// Delete elements (based on node location)
  this.deleteByNode = function (point) {
    if (point > -1 && point < length) {
      let current = head
      let index = 0
      let prev = null

      if (point === 0) {
        head = current.next;
        length--
        return current.point
      } else {
        while (index++ < point) {
          prev = current;
          current = current.next;
        }
        prev.next = current.next;
        // console.log(head === prev) // true points to the same object
        length--;
        returncurrent.val; }}else {
      return null; }}// Return the length of the list
  this.size = function () {
    return length;
  }
  // Display the linked list
  this.show = function () {
    let current = head;
    let str = ' '
    while (current) {
      str += current.val
      current = current.next;
      if (current) {
        str += '- >'}}// javascript -> html -> css -> react -> vue
    return str
  };
  // Check whether the list is empty
  this.isEmpty = function () {
    return length === 0
  }
  // Get the head node
  this.getHead = function () {
    returnhead; }}const linked = new ListNode();
linked.add("0");
linked.add("1");
linked.add("2");
linked.add("3");
linked.add("4");
linked.insert("5".5)



console.log("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- show list -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --")
console.log(linked.show())

/ / the console. The log (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the editor list -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
// console.log(linked.edit("2", "22"))
// console.log(linked.show())

/ / the console. The log (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- whether the list is empty -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
// console.log(linked.isEmpty());

/ / the console. The log (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to get head node -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
// console.dir(linked.getHead(), { depth: 100 });

/ / the console. The log (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the node number -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
// console.log(linked.size());

. / / the console log (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- query element subscript -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
// console.log(linked.indexOf("3"));

/ / the console. The log (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- query node According to the node element -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
// console.log(linked.find("0"));

/ / the console. The log (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- query node According to the node position -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
// console.log(linked.findByNode(0))

/ / the console. The log (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- precursor node -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
// console.log(linked.findPre("0"));

/ / the console. The log (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- precursor node According to the node position -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
// console.log(linked.findPreByNode(5));

. / / the console log (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- according to the element to delete nodes -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
// console.log(linked.delete("6"))
// console.log(linked.size());
// console.log(linked.show())

/ / the console. The log (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- according to node delete -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ")
// console.log(linked.deleteByNode(1))
// console.log(linked.size());
// console.log(linked.show())


// Set head to why? // set head to why?
let obj1 = { test: "1" }
let obj2 = { test: "1" }
// console.log(obj1 === obj2) // false pointer pointing points to different addresses
// === = Two compound types (object, array, function) are compared not to see if their values are equal, but to see if they refer to the same object.
let obj3 = { test: "1" }
let obj4 = obj3
// console.log(obj3 === obj4) //true
obj4.test = "2"
 // console.log(obj3) //{ test: '2' }
Copy the code