JS Single Necklace table

Source: Internet
Author: User

Introducing the linked list

A linked list is a collection of nodes that are made up of a group. Each node uses a reference to an object to point to its subsequent borrowing points. A reference to another borrowing point is called a chain.
In many programming languages, the length of an array is fixed, that is, when you define an array, you need to define the length of the array, so it is difficult to add new elements when the array is already filled with data. It can only be said that this is the case in some languages, and the length of the array in JavaScript and PHP is arbitrarily increased. Adding and removing elements in an array is also cumbersome because it is not a problem in JavaScript to translate other elements in the array forward or backward, and there is a handy way to add or remove elements in JavaScript splice ().
But everything is relative, and the arrays in JavaScript have their own problems, they are designed as objects that are inefficient compared to other languages such as C + + and Java.
If the efficiency of an array is slow to find in the actual use, you can consider using a linked list instead. An array has the advantage of being able to access the values of the array conveniently based on the key value, and in addition, the list can be substituted for the array in any situation. If you need to randomly access elements, the array is still a better choice.

Code implementation

Defining a linked List node class
The node class contains two attributes, element is used to hold the data on the node, and next holds the link to the next node

class Node {    constructor(element) {        this.element = element;  // 表示节点上的数据        this.next = null;   // 表示指向下一个节点的链接    }}export default Node;

The

Define linked list Class (LinkedList) class
LinkedList class provides an insertion node, a method for deleting nodes, displaying the elements of a linked list node, and some other helper methods

Import node from './node ';/** * Linked List class */class LinkedList {/** * constructor, initialize head node */constructor () {  This.head = new Node (' head '); NOD List}/** * Find current node based on specified value * Helper method * @param {*} item */_find (item) {Let Currnode = thi        S.head;        while (currnode.element! = Item) {Currnode = Currnode.next;        } return Currnode; /** * Insert a new node after the specified node * @param {*} newelement Insert new node * @param {*} Item Insert element's previous node value */Insert (Newel        Ement, item) {Let NewNode = new Node (newelement);        Let Currnode = This._find (item);        Newnode.next = Currnode.next;    Currnode.next = NewNode; /** * Remove Node * @param {*} Item */Remove (item) {///Find the previous node of the specified node let PrevNode = This.h        ead        while (Prevnode.next = null && prevNode.next.element! = Item) {PrevNode = Prevnode.next; } if (Prevnode.next! = null) {// Set the previous node next prevnode.next = PrevNode.next.next, which points to the current node;        }}/** * Displays all node data */display () {Let currnode = This.head;            while (currnode.next! = null) {Console.log (currNode.next.element);        Currnode = Currnode.next; }}}export default LinkedList;
Test
import LinkedList from './LinkedList';// 创建一个 LinkedList 实例let ll = new LinkedList();// 插入一些元素ll.insert('zhangsan', 'head');ll.insert('lisi', 'zhangsan');ll.insert('wangwu', 'lisi');ll.display();  // 显示元素,控制台输出 zhangsan,lisi,wangwull.remove('lisi');ll.display(); // 控制台输出 zhangsan,wangwu

JS Single Necklace table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.