JavaScript Tutorial--Implementation of Linked List Data Structure
Linked list is a common basic data structure. It is a linear list, but it does not store data in linear order. Instead, it stores a pointer to the next node in each node. ). Below we use JavaScript code to implement the data structure of the linked list
The linked list (Linked list) is a common basic data structure and a linear table, but it does not store data in a linear order. It is a pointer (Pointer) stored in each node to the next node - Wikipedia
The above is Wikipedia's interpretation of the linked list. Below we use JavaScript code to implement the data structure of the linked list
Implement the Node class to represent the node
/** * Node 类用来表示节点 * element 用来保存节点上的数据 * next 用来保存指向下一个节点的链接 */ function Node(element) { this.element = element; this.next = null; } LList类提供对链表操作的方法 /** * LList 类提供了对链表进行操作的方法 * 链表只有一个属性, * 使用一个 Node 对象来保存该链表的头节点。 */ class LList { constructor() { this.head = new Node('head'); } // 查找节点 find(item) { let currNode = this.head; while(currNode.element !== item) { currNode = currNode.next; } return currNode; } // 查找前一个节点 findPre(item) { if(item === 'head') throw new Error('now is head!'); let currNode = this.head; while (currNode.next && currNode.next.element !== item) { currNode = currNode.next; } return currNode; } // 插入新节点 insert(newElement, item) { let newNode = new Node(newElement); let currNode = this.find(item); newNode.next = currNode.next; currNode.next = newNode; } // 删除一个节点 remove(item) { let preNode = this.findPre(item); if(preNode.next !== null) { preNode.next = preNode.next.next; } } // 显示链表中的元素 display() { let currNode = this.head; while(currNode.next !== null) { console.log(currNode.next.element); currNode = currNode.next; } } }
Test code
const list = new LList(); // LList { head: Node { element: 'head', next: null } } list.insert('0', 'head'); list.insert('1', '0'); list.insert('2', '1'); list.insert('3', '2'); list.remove('1'); console.log(list); // LList { head: Node { element: 'head', next: Node { element: '0', next: [Object] } } } console.log(list.display()); // 0 2 3 console.log(list.findPre('1')); // Node { element: '0', next: Node { element: '1', next: Node { element: '2', next: [Object] } } }
The above is a simple implementation of the data structure of a simple linked list using JavaScript: smile:
The above is the detailed content of JavaScript Tutorial--Implementation of Linked List Data Structure. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

When using complex data structures in Java, Comparator is used to provide a flexible comparison mechanism. Specific steps include: defining the comparator class, rewriting the compare method to define the comparison logic. Create a comparator instance. Use the Collections.sort method, passing in the collection and comparator instances.

Data structures and algorithms are the basis of Java development. This article deeply explores the key data structures (such as arrays, linked lists, trees, etc.) and algorithms (such as sorting, search, graph algorithms, etc.) in Java. These structures are illustrated through practical examples, including using arrays to store scores, linked lists to manage shopping lists, stacks to implement recursion, queues to synchronize threads, and trees and hash tables for fast search and authentication. Understanding these concepts allows you to write efficient and maintainable Java code.

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Reference types are a special data type in the Go language. Their values do not directly store the data itself, but the address of the stored data. In the Go language, reference types include slices, maps, channels, and pointers. A deep understanding of reference types is crucial to understanding the memory management and data transfer methods of the Go language. This article will combine specific code examples to introduce the characteristics and usage of reference types in Go language. 1. Slices Slices are one of the most commonly used reference types in the Go language.

AVL tree is a balanced binary search tree that ensures fast and efficient data operations. To achieve balance, it performs left- and right-turn operations, adjusting subtrees that violate balance. AVL trees utilize height balancing to ensure that the height of the tree is always small relative to the number of nodes, thereby achieving logarithmic time complexity (O(logn)) search operations and maintaining the efficiency of the data structure even on large data sets.

Overview of Java Collection Framework The Java collection framework is an important part of the Java programming language. It provides a series of container class libraries that can store and manage data. These container class libraries have different data structures to meet the data storage and processing needs in different scenarios. The advantage of the collection framework is that it provides a unified interface, allowing developers to operate different container class libraries in the same way, thereby reducing the difficulty of development. Data structures of the Java collection framework The Java collection framework contains a variety of data structures, each of which has its own unique characteristics and applicable scenarios. The following are several common Java collection framework data structures: 1. List: List is an ordered collection that allows elements to be repeated. Li

Introduction to JS-Torch JS-Torch is a deep learning JavaScript library whose syntax is very similar to PyTorch. It contains a fully functional tensor object (can be used with tracked gradients), deep learning layers and functions, and an automatic differentiation engine. JS-Torch is suitable for deep learning research in JavaScript and provides many convenient tools and functions to accelerate deep learning development. Image PyTorch is an open source deep learning framework developed and maintained by Meta's research team. It provides a rich set of tools and libraries for building and training neural network models. PyTorch is designed to be simple, flexible and easy to use, and its dynamic computation graph features make
