


Sharing examples of JavaScript data structures singly linked list and circular linked list
This article mainly introduces the JavaScript data structures of singly linked lists and circular linked lists, and introduces in detail how JavaScript implements singly linked lists and circular linked lists. If you are interested, you can learn more. I hope it can help everyone.
Getting to the point, here is a brief introduction to the data structure knowledge of linked lists:
The linked list is a non-linear and non-continuous data structure in physical storage units (it is in the data logic is linear), each of its nodes consists of two domains: data domain and pointer domain. The actual data is stored in the data field, and the pointer field stores pointer information, pointing to the next element or the previous element in the linked list. It is precisely because of the existence of pointers that the storage of linked lists is discontinuous in physical units.
The advantages and disadvantages of linked lists are equally obvious. Compared with linear lists, linked lists are more efficient in adding and deleting nodes, because they only need to modify pointer information to complete the operation, unlike linear lists (arrays) that require moving elements. Similarly, the length of a linked list is theoretically infinite (within the memory capacity), and the length can be changed dynamically, which has great advantages over linear lists. Correspondingly, since linear tables cannot randomly access nodes and can only be accessed through pointer traversal queries along the linked list, the efficiency of accessing data elements is relatively low.
The following is the JS part
The common methods and descriptions encapsulated here:
Methods | Description |
---|---|
append(element) | Add node element to the end of the linked list |
insert(position,element) | Insert node element to position position |
removeAt(position) | Delete node according to index value position |
remove(element) | Search and delete the given node element |
remove() | Delete the last node in the linked list |
indexOf(element) | Find and return the index value of the given node element |
isEmpty() | Judge whether the linked list is empty |
Get the length of the linked list | |
Convert to string output | |
Get the head node | |
Get the tail node |
function LinkedList(){ /*节点定义*/ var Node = function(element){ this.element = element; //存放节点内容 this.next = null; //指针 } var length = 0, //存放链表长度 head = null; //头指针 this.append = function(element){ var node = new Node(element), current; //操作所用指针 if (!head){ head = node; }else { current = head; while(current.next){ current = current.next; } current.next = node; } length++; return true; }; this.insert = function(position, element){ if (position >= 0 && position <= length) { var node = new Node(element), current = head, previous, index = 0; if(position === 0){ node.next = current; head = node; }else{ while(index++ < position){ previous = current; current = current.next; } node.next = current; previous.next = node; } length++; return true; }else{ return false; } }; this.removeAt = function(position){ if(position > -1 && position < length){ var current = head, previous, index = 0; if (position === 0) { head = current.next; }else{ while (index++ < position){ previous = current; current = current.next; } previous.next = current.next; }; length--; return current.element; }else{ return null; } }; this.remove = function(element){ var current = head, previous; if(element === current.element){ head = current.next; length--; return true; } previous = current; current = current.next; while(current){ if(element === current.element){ previous.next = current.next; length--; return true; }else{ previous = current; current = current.next; } } return false; }; this.remove = function(){ if(length < 1){ return false; } var current = head, previous; if(length == 1){ head = null; length--; return current.element; } while(current.next !== null){ previous = current; current = current.next; } previous.next = null; length--; return current.element; }; this.indexOf = function(element){ var current = head, index = 0; while(current){ if(element === current.element){ return index; } index++; current = current.next; } return false; }; this.isEmpty = function(){ return length === 0; }; this.size = function(){ return length; }; this.toString = function(){ var current = head, string = ''; while(current){ string += current.element; current = current.next; } return string; }; this.getHead = function(){ return head; } }Circular linked list: Based on the singly linked list, point the pointer of the tail node to the head node to form A circular linked list. Starting from any node in a circular linked list, the entire linked list can be traversed.
function CircularLinkedList(){ var Node = function(element){ this.element = element; this.next = null; } var length = 0, head = null; this.append = function(element){ var node = new Node(element), current; if (!head) { head = node; node.next = head; }else{ current = head; while(current.next !== head){ current = current.next; } current.next = node; node.next = head; }; length++; return true; }; this.insert = function(position, element){ if(position > -1 && position < length){ var node = new Node(element), index = 0, current = head, previous; if (position === 0) { node.next = head; head = node; }else{ while(index++ < position){ previous = current; current = current.next; } previous.next = node; node.next = current; }; length++; return true; }else{ return false; } }; this.removeAt = function(position){ if(position > -1 && position < length){ var current = head, previous, index = 0; if (position === 0) { head = current.next; }else{ while (index++ < position){ previous = current; current = current.next; } previous.next = current.next; }; length--; return current.element; }else{ return null; } }; this.remove = function (element){ var current = head, previous, indexCheck = 0; while(current && indexCheck < length){ if(current.element === element){ if(indexCheck == 0){ head = current.next; length--; return true; }else{ previous.next = current.next; length--; return true; } }else{ previous = current; current = current.next; indexCheck++; } } return false; }; this.remove = function(){ if(length === 0){ return false; } var current = head, previous, indexCheck = 0; if(length === 1){ head = null; length--; return current.element; } while(indexCheck++ < length){ previous = current; current = current.next; } previous.next = head; length--; return current.element; }; this.indexOf = function(element){ var current = head, index = 0; while(current && index < length){ if(current.element === element){ return index; }else{ index++; current = current.next; } } return false; }; this.isEmpty = function(){ return length === 0; }; this.size = function(){ return length; }; this.toString = function(){ var current = head, string = '', indexCheck = 0; while(current && indexCheck < length){ string += current.element; current = current.next; indexCheck++; } return string; }; }Usage method:
Examples of using doubly linked lists in JavaScript data structures
##JavaScript Priority queue and circular queue in data structureDetailed explanation of the definition and representation method of binary search tree of JavaScript data structureThe above is the detailed content of Sharing examples of JavaScript data structures singly linked list and circular linked list. 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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
