# DS with JS — Linked Lists — II

## Data Structures with JavaScript — Chapter Two — Doubly Linked Lists

A doubly linked list is a linked data structure that consists of a set of sequentially linked records callednodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes.

⚠️If you wan’t to learn more about linked-listsclick here

#### Before Proceeding further…

This post is continued from **DS with JS — Linked Lists****, **If you haven’t gone through that yet, make sure you do before proceeding further…

### Core

### ⚜️ The list

*Append**AppendAt**Remove**RemoveAt**Reverse**Swap**IsEmpty & Length**Traverse**Display**Search*

### Lets’ begin!!

❗️All the code snippets you will see below are just Pseudo code. If you want to see working code — click here

#### 🔅 Append

append(item){

letnode=newNode( item );

if(!this.head) {

this.head=node;

this.tail=node;

}else{

node.prev=this.tail;

this.tail.next=node;

this.tail=node

}}

#### 🔅 AppendAt

appendAt(pos,item){

letcurrent=this.head;

letcounter=1;

letnode=newNode( item );

if( pos==0 ) {

this.head.prev=node

node.next=this.head

this.head=node

}else{

while(current) {

current=current.next;

if( counter==pos ) {

node.prev=current.prev

current.prev.next=node

node.next=current

current.prev=node

}

counter++

}

}}

#### 🔅 Remove

remove(item){

letcurrent=this.head;

while( current ) {

if( current.data===item ) {

if( current==this.head&¤t==this.tail ) {

this.head =null;

this.tail =null;

}elseif( current==this.head ) {

this.head=this.head.next

this.head.prev=null

}elseif( current==this.tail ) {

this.tail=this.tail.prev;

this.tail.next=null;

}else{

current.prev.next=current.next;

current.next.prev=current.prev;

}

}

current=current.next

}}

#### 🔅 RemoveAt

removeAt(pos) {

letcurrent=this.head;

letcounter=1;

if( pos==0 ) {

this.head=this.head.next;

this.head.prev=null;

}else{

while( current ) {

current=current.next

if( current==this.tail ) {

this.tail =this.tail.prev;

this.tail.next=null;

}elseif( counter==pos ) {

current.prev.next=current.next;

current.next.prev=current.prev;

break;

}

counter++;

}

}}

#### 🔅 Reverse

reverse(){

letcurrent=this.head;

letprev=null;

while( current ){

letnext=current.next

current.next=prev

current.prev=next

prev=current

current=next

}

this.tail=this.head

this.head=prev}

#### 🔅 Swap

swap(nodeOne,nodeTwo){

letcurrent=this.head;

letcounter=0;

letfirstNode;

while( current!==null ) {

if( counter==nodeOne ){

firstNode=current;

}elseif( counter==nodeTwo ) {

lettemp=current.data;

current.data=firstNode.data;

firstNode.data=temp;

}

current=current.next;

counter++;

}

returntrue}

#### 🔅 IsEmpty & Length

length() {

letcurrent=this.head;

letcounter=0;

while( current!==null) {

counter++

current=current.next

}

returncounter;

}

isEmpty(){

returnthis.length()<1}

#### 🔅 Traverse

traverse(fn){

letcurrent=this.head;

while( current!==null) {

fn(current)

current=current.next;

}

returntrue;}

#### 🔅 TraverseReverse

traverseReverse(fn){

letcurrent=this.tail;

while( current!==null) {

fn(current)

current=current.prev;

}

returntrue;}

#### 🔅 Search

search(item){

letcurrent=this.head;

letcounter=0;

while( current ) {

if( current.data==item ) {

returncounter

}

current=current.next

counter++

}

returnfalse;}

### About this post

This post is the second instalment to its series “**DS with JS**”. There will be more in this series. Next will be on **next Thursday Morning**. **Stay tuned!**

### That’s It

#### Happy Coding !!

🎧 No music today… I lost something very important today…

*If you like this article, please give it some claps *👏* and share it! If you do not like this post or have any type of questions regarding anything that i mentioned in this post. Feel free to ask me. Just post an issue in my “**Ask Me Anything” **by **clicking here**.*

*For more like this, **follow me on Medium** or **Twitter**. To ask a Question **visit this link**. More about me on my **website**.*