This article mainly introduces the definition and use of doubly linked lists in JavaScript data structures. It briefly introduces the principles of doubly linked lists, and analyzes the definition and use of doubly linked lists in the form of examples. Friends who need it can refer to it. I hope it can help. to everyone.
The difference between a doubly linked list and an ordinary linked list is that in a linked list, a node only has a link to the next node, while in a doubly linked list, the links are two-way: one link leads to the next element, and the other Chain to the previous element.
Doubly linked lists provide two methods of iterating the list: from beginning to end, or vice versa. We can also access the next or previous element of a specific node. In a one-way linked list, if you miss the element you are looking for while iterating the list, you need to go back to the starting point of the list and start iterating again. This is an advantage of doubly linked lists.
function DoubleLink(){ var length=0;//链表长度 var head=null;//头结点的引用 var tail=null;//尾节点的引用 function Node(e){ this.element=e; this.next=null; this.previous=null; } this.insertAt=function(position,e){//在任意位置添加节点 if(position>=0&&position<=length){//判断边界 var node=new Node(e); var current=head; var previous; var index=0; if(position==0){//在第一个位置添加 if(!head){//链表为空的时候添加第一个节点 head=node; tail=node; }else{ current=head; node.next=current; current.previous=node; head=node; } }else if(position==length){//在链表末尾添加 current=tail; current.next=node; node.previous=current; tail=node; }else{ while(index<position){ previous=current; current=current.next; index++; } previous.next=node; node.previous=previous; node.next=current; current.previous=node; } length++; return true; }else{ return null; } } this.removeAt=function(position){//删除任意位置的节点 if(position>-1&&position<length){//边界判断 var current=head; var previous; var index=0; if(position==0){//删除第一个位置的节点 head=current.next; if(length==1){//如果只有一项 tail=null; }else{ head.previous=null; } }else if(position==length-1){//删除最后一项 current=tail; tail=current.previous; tail.next=null; }else{ while(index<position){ previous=current; current=current.next; index++; } previous.next=current.next; current.next.previous=previous; } length--; return current.element; }else{ return null; } } this.indexOf=function(e){//获取节点位置,从头开始数 var current=head; var index=0; while(current){ if(current.element==e){ return index; } current=current.next; index++; if(index>=length)return null; } } this.isEmpty=function(){//判断链表是否为空 return length==0; } this.mylength=function(){//链表长度 return length; } this.print1=function(){//从头到尾打印链表 var current=head; while(current){ console.log(current.element); current=current.next; } } this.print2=function(){//从尾到头打印链表 var current=tail; while(current){ console.log(current.element); current=current.previous; } } this.getHead=function(){//获取头节点 return head; } this.getTail=function(){//获取尾节点 return tail; } } var link=new DoubleLink();//实例化一个对象 link.insertAt(0,'d'); link.insertAt(1,'e'); link.insertAt(2,'f'); link.insertAt(3,'g'); link.insertAt(4,'h'); link.insertAt(5,'i'); link.insertAt(6,'j'); link.insertAt(7,'k'); link.removeAt(7); link.removeAt(0); link.print1();//efghij link.print2();//jihgfe console.log(link.getHead());//e console.log(link.getTail());//j console.log(link.indexOf('f'));//1
Running results:
Related recommendations:
Realization of doubly linked list and doubly circular linked list in JavaScript
PHP short tutorial on implementing doubly linked list _PHP tutorial
The above is the detailed content of JavaScript doubly linked list definition and usage. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
