Code Monkey home page Code Monkey logo

Comments (1)

MMmaXingXing avatar MMmaXingXing commented on August 13, 2024

什么是链表

链表是一种物理存储单元上非连续,非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针连接次序实现的。链表由一系列节点组成,节点可以在运行时动态生成。每个节点包含两个部分: 一个是存储节点的数据域(当前的数据),另一个是节点地址的指针域(next)。链表允许插入和移除表上任意位置的节点,但是不允许随机存取。

链表有,单向链表、双向链表、循环链表等。

实现一个链表

// 简易单向链表
class Node {
  constructor(element) {
    this.element = element;
    this.next = null;
  }
}
class NodeList {
  constructor(item) {
    this.head = new Node(item); // 初始化链表的头节点
  }

  append(item) {
    let newNode = new Node(item);
    let lastNode = this.findLastNode();
    lastNode.next = newNode;
  }

  insert(newItem, beforeItem) {
    let newNode = new Node(newItem);

    if (beforeItem) {
      let currNode = this.findNode(beforeItem);
      newNode.next = currNode.next;
      currNode.next = newNode;
    } else {
      let lastNode = this.findLastNode();
      lastNode.next = newNode;
    }
  }

  findNode(item) {
    // 根据元素查找节点
    let currNode = this.head;
    while (currNode.element != item && currNode) {
      if (currNode.next) {
        currNode = currNode.next;
      } else {
        currNode = null;
      }
    }

    return currNode;
  }

  findLastNode() {
    let currNode = this.head;
    while (currNode.next) {
      currNode = currNode.next;
    }
    return currNode;
  }

  remove(item) {
    let preNode = this.findNode(item);
    if (preNode.next != null) {
      preNode.next = preNode.next.next;
    }
  }

  findPreNode(item) {
    let currNode = this.head;
    while (currNode & currNode.next & (currNode.next.element != item)) {
      if (currNode.next) {
        currNode = currNode.next;
      } else {
        currNode = null;
      }
    }
    return currNode;
  }

  toString() {
    let currNode = this.head;
    let strList = [];
    while (currNode.next) {
      strList.push(JSON.stringify(currNode.element));
      currNode = currNode.next;
    }
    strList.push(JSON.stringify(currNode.element));
    return strList.join("->");
  }

  toReverseString() {
    let currNode = this.head;
    let strList = [];
    while (currNode.next) {
      strList.push(JSON.stringify(currNode.element));
      currNode = currNode.next;
    }
    strList.push(JSON.stringify(currNode.element));
    strList.reverse();
    return strList.join("->");
  }
}

// toReverseString 可用于倒序输出链表(可用双向链表实现 待续)

双向链表实现以上题目

class LNode {
  constructor(element, previous) {
    this.element = element;
    this.next = null;
    this.previous = null;
  }
}

class NodeLList {
  constructor(item) {
    this.head = new LNode(item); // 初始化链表的头节点
  }

  findNode(item) {
    // 根据元素查找节点
    let currNode = this.head;
    while (currNode.element != item && currNode) {
      if (currNode.next) {
        currNode = currNode.next;
      } else {
        currNode = null;
      }
    }

    return currNode;
  }

  findLastNode() {
    let currNode = this.head;
    while (currNode.next) {
      currNode = currNode.next;
    }
    return currNode;
  }

  insert(newItem, beforeItem) {
    let newNode = new LNode(newItem);

    if (beforeItem) {
      let currNode = this.findNode(beforeItem);
      newNode.next = currNode.next;
      newNode.previous = currNode;
      currNode.next = newNode;
    } else {
      let lastNode = this.findLastNode();
      newNode.previous = lastNode;
      lastNode.next = newNode;
    }
  }

  revertToString() {
    let currNode = this.findLastNode();
    console.log(currNode);
    let strList = [];
    while (currNode.previous) {
      strList.push(JSON.stringify(currNode.element));
      currNode = currNode.previous;
    }
    strList.push(JSON.stringify(currNode.element));
    return strList.join("->");
  }
}

let index1 = 1;
let index2 = 2;
let index3 = 3;

let nList = new NodeLList(index1);

nList.insert(index2);
nList.insert(index3);

console.log("" + nList.revertToString());

from daily.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.