Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.

Commit 97b4285

Browse files
Merge pull request #805 from dius00/dataStructuresJs
JS - Data Stuctures - Linked/Doubly Linked List sample code.
2 parents cc2051d + 4d047f0 commit 97b4285

3 files changed

+137
-0
lines changed

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ If you have contributed to this repository, kindly add your username here
6161
- [aman-ku](https://github.com/aman-ku)
6262
- [anbclausen](https://github.com/anbclausen)
6363
- [Pulkit Aggarwal](https://github.com/PulkitAgg)
64+
- [dius00](https://github.com/dius00)

js/dius00_dataStructure_LinkedList.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Javascript ES6 syntax code sample for simple Linked List
2+
3+
4+
/*
5+
* Creates the Linked List
6+
* if a value is given to the constructor
7+
* the element will be initialized as a Node (below)
8+
*/
9+
class LinkedList {
10+
constructor(value){
11+
if(value !== undefined){
12+
this.head = new Node(value);
13+
this.tail = this.head;
14+
}
15+
}
16+
17+
/*
18+
* Method to add a new Node to the list
19+
*/
20+
addNode(value){
21+
const newNode = new Node (value); // Generates a new Node element
22+
if(!this.head) { // if there is no head make the new Node the head of the list
23+
this.head = newNode
24+
} else { // if there is a head, link the new Node to the tail of the list
25+
this.tail.next = newNode;
26+
this.tail = newNode;
27+
}
28+
}
29+
30+
/*
31+
* Method to remove the head
32+
*/
33+
34+
removeHead() {
35+
const oldHead = this.head;
36+
this.head = this.head.next;
37+
return oldHead;
38+
}
39+
40+
/*
41+
* Method to find a specific Node in the list
42+
*/
43+
44+
findNode(value) {
45+
let iterator = this.head;
46+
while (iterator) {
47+
if (iterator.value === value) return iterator;
48+
iterator = iterator.next;
49+
}
50+
return null;
51+
}
52+
}
53+
54+
55+
/*
56+
* Method to find a specific Node in the list
57+
*/
58+
59+
class Node {
60+
constructor(value){
61+
this.value = value;
62+
this.next = null;
63+
}
64+
}
65+
66+
// if you want to export this Class
67+
// module.exports = LinkedList;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Javascript ES6 syntax code sample for Doubly-Linked List
2+
3+
4+
/*
5+
* Creates the Doubly-Linked List
6+
* if a value is given to the constructor
7+
* the element will be initialized as a Node (below)
8+
*/
9+
class DoublyLinkedList {
10+
constructor(value){
11+
if(value !== undefined){
12+
this.head = new Node(value);
13+
this.tail = this.head;
14+
}
15+
}
16+
17+
/*
18+
* Method to add a new Node to the list
19+
*/
20+
addNode(value){
21+
const newNode = new Node (value); // Generates a new Node element
22+
if(!this.head) { // if there is no head make the new Node the head of the list
23+
this.head = newNode
24+
} else { // if there is a head, link the new Node to the tail of the list
25+
this.tail.next = newNode;
26+
newNode.prev = this.tail;
27+
this.tail = newNode;
28+
}
29+
}
30+
31+
/*
32+
* Method to remove the head
33+
*/
34+
35+
removeHead() {
36+
const oldHead = this.head;
37+
this.head = this.head.next;
38+
return oldHead;
39+
}
40+
41+
/*
42+
* Method to find a specific Node in the list
43+
*/
44+
45+
findNode(value) {
46+
let iterator = this.head;
47+
while (iterator) {
48+
if (iterator.value === value) return iterator;
49+
iterator = iterator.next;
50+
}
51+
return null;
52+
}
53+
}
54+
55+
56+
/*
57+
* Method to find a specific Node in the list
58+
*/
59+
60+
class Node {
61+
constructor(value){
62+
this.value = value;
63+
this.next = null;
64+
this.prev = null;
65+
}
66+
}
67+
68+
// if you want to export this Class
69+
// module.exports = DoublyLinkedList;

0 commit comments

Comments
 (0)