Data Structures in JavaScript

ARRAYS

/ / creating an array
var users=["user 1", "user 2", "user 3"];
/ / iterate over an array
for (i=0;i<users . length;i++){
document.write (users[i] +" , ");
}
output = user 1, user 2, user 3

Here are 7 Javascript Data Structures you must know

LINKED LIST

reverse(){
var node=this.head;
this.head = this.tail;
this.tail = node;
var next;
var prev = null;
for (var i = 0; i < this.length; i++){
next= node.next;
node.next = prev;
prev = node;
node = next;
}
return this;
}
 / /.reverse () method of a singly linked list

STACK

let stack = [ ];
 stack. push(1) ;
console.log (stack) ; / / [1]

stack.push (2);
console. log (stack); / / [1,2]

stack.push (3) ;
console. log (stack) ; / / [1,2,3]
stack.push (4) ;
console. log (stack) ; / /  [1,2,3,4]
stack.push (5) ;
console. log (stack) ; / /  [1,2,3,4, 5]

QUEUE

class Queue {
constructor() {
this.items = { };
this.head Index = 0;
this.tailIndex = 0;
}
enqueue (item) {
this.items[this.tailIndex] = item;
this.tailIndex++;
}
dequeue() {
const item = this.items [this.head Index];
delete this. items [this .headIndex];
this.head index++ ;
return item;
}
peek()
return this. items [this.headIndex];
}
get length () {
return this.tailIndex this . headIndex;
}
}
 const queue = new Queue (); // how you create an instance of queue
 queue.enqueue(7);
queue.enqueue(2);
queue.enqueue(6);
 queue.enqueue (4);
console. log(queue.dequeue ()); // => 7
console . log(queue. peek()); / / => 2
 console. log (queue . length ) / / =>3

TREE

class TreeNode {
constructor (value) {
this.value = value;
this.descendants = [ ];
}
}
/ / create nodes with values
const marge = new TreeNode( ' Marge ');
const homer = new TreeNode ('Homer ');
const bart = new TreeNode( 'Bart ');
const lisa = new TreeNode( 'Lisa' );
const maggie = new TreeNode ( 'Maggie');
/ / associate root with is descendants
marge.descendants.push(homer);
homer. descendants. push (bart, lisa, maggie) ;

GRAPHS

class Graph{
constructor(){
this.adjacencyList = {};
}
addVertex(vertex) {
if (!this.adjacencyList[vertex]){
this.adjacencyList [vertex] = [ ];
}
}
addEdge(source, destination){
if (!this. adjacencyList [ source] ) {
this. addVertex(source) ;
}
if (!this .adjacencyList [destination ] ) {
this .addVertex (destination) ;
}
this.adjacencyList[source].push(destination) ;
this.adjacencyList [destination].push(source) ;
}
removeEdge (source, destination) {
this.adjacencyList[source] this.adjacencyList [ source ].filter(vertex => vertex ! == destination);
this.adjacencyList[destination] = this.adjacencyList [destination].filter(vertex => vertex !== source);
}
removeVertex(vertex){
while (this .adjacencyList [vertex]) {
const adjacentVertex = this.adjacencyList [ vertex].pop() ;
this.removeEdge (vertex, adjacentVertex);
}
delete this .adjacencyList [vertex];
}
}

HASH TABLE

class HashTable{
//Constructor
constructor ( ){
/ /Size of the HashTable
this.slots =10;
//Current entries in the table
/ / Used while resizing the table when half of the table gets filled this.size = 0;
/ / Array of HashEntry objects (by default all None)
this.bucket = [ ];
for (var i=0; i<this. slots; i++){
this.bucket [i]=null; 
}
}

//Helper Functions
get_size (){
return this.size;
}
isEmpty ( ){
return this.get_size( ) == 0;
}
}
 let ht = new HashTable ( );
console. log (ht . isEmpty ());



Leave a Reply

Your email address will not be published.