Skip to content

Commit 2f4a9f1

Browse files
author
Yashendra Goyal
committed
first commit
0 parents  commit 2f4a9f1

File tree

10 files changed

+287
-0
lines changed

10 files changed

+287
-0
lines changed

array.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const myArr = [1, "Raj", [1,2], {name: "XYZ"}];
2+
3+
// Insertion
4+
// Push
5+
myArr.push(3);
6+
7+
console.log(myArr);
8+
9+
// Unshift
10+
11+
myArr.unshift([2]);
12+
13+
// console.log(myArr);
14+
15+
// console.log(myArr[3]);
16+
17+
// Delete
18+
19+
// Pop
20+
myArr.pop();
21+
22+
console.log(myArr);
23+
24+
myArr.shift();
25+
26+
console.log(myArr);
27+
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const DoublyListNode = require("./doubly-list-node");
2+
3+
function DoublyLinkedList() {
4+
this.size = 0;
5+
this.head = new DoublyListNode(0);
6+
this.tail = new DoublyListNode(0);
7+
this.head.next = this.tail;
8+
this.tail.prev = this.head; // head ==== tail
9+
}
10+
11+
// Add at index
12+
DoublyLinkedList.prototype.addAtIndex = function(index, value) {
13+
const { size } = this;
14+
if (index < 0 || index > size) {
15+
return;
16+
}
17+
18+
let prev = this.head;
19+
let next = this.tail;
20+
21+
if (index < size - index) {
22+
while (index) {
23+
prev = prev.next;
24+
index--;
25+
}
26+
next = prev.next;
27+
} else {
28+
while(size - index) {
29+
next = next.prev;
30+
index++;
31+
}
32+
prev = next.prev;
33+
}
34+
35+
const newNode = new DoublyListNode(value);
36+
newNode.prev = prev;
37+
newNode.next = next;
38+
prev.next = newNode;
39+
next.prev = newNode;
40+
this.size++;
41+
}
42+
43+
DoublyLinkedList.prototype.addAtHead = function(value) {
44+
return this.addAtIndex(0, value);
45+
}
46+
47+
DoublyLinkedList.prototype.getAtIndex = function(index) {
48+
const { size } = this;
49+
if (index < 0 || index >= size) {
50+
return null;
51+
}
52+
53+
if (index < size - index) {
54+
let node = this.head;
55+
while (index >= 0) {
56+
node = node.next;
57+
index--;
58+
}
59+
return node;
60+
} else {
61+
let node = this.tail;
62+
while(size - index) {
63+
node = node.prev;
64+
index++;
65+
}
66+
return node;
67+
}
68+
}
69+
70+
DoublyLinkedList.prototype.deleteAtIndex = function(index) {
71+
const { size } = this;
72+
if (index < 0 || index >= size) {
73+
return null;
74+
}
75+
76+
let nodeToDelete = this.head;
77+
if (index < size - index) {
78+
while (index >= 0) {
79+
nodeToDelete = nodeToDelete.next;
80+
index--;
81+
}
82+
} else {
83+
nodeToDelete = this.tail;
84+
while(size - index) {
85+
nodeToDelete = nodeToDelete.prev;
86+
index++;
87+
}
88+
}
89+
console.log('NODE TO DELETE>>', nodeToDelete);
90+
const prevNode = nodeToDelete.prev;
91+
const nextNode = nodeToDelete.next;
92+
prevNode.next = nextNode;
93+
nextNode.prev = prevNode;
94+
nodeToDelete.prev = null;
95+
nodeToDelete.next = null;
96+
this.size--;
97+
}
98+
99+
module.exports = DoublyLinkedList
100+
101+
102+
// ind 0, size 3, size - index = 3
103+
// ind 3, size 3, size - index = 0
104+
// ind 1, size 3, size - index = 2
105+
// ind 2, size 3, size - index = 1
106+
// head ==== 3 ==== 4 === 5 === tail
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function DoublyListNode(value) {
2+
this.value = value;
3+
this.next = null;
4+
this.prev = null;
5+
}
6+
7+
module.exports = DoublyListNode;

doubly-linked-list/test-dll.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const DoublyLinkedList = require("./doubly-linked-list");
2+
3+
const dll = new DoublyLinkedList();
4+
5+
dll.addAtHead(4);
6+
dll.addAtHead(5);
7+
dll.addAtHead(6); // 6 --> 5 ---> 4
8+
// console.log(dll.head.next);
9+
dll.addAtIndex(1, 12); // 6 === 12 === 5 === 4
10+
// console.log(dll.head.next);
11+
dll.deleteAtIndex(2);
12+
// console.log(dll.head.next);
13+
console.log(dll.getAtIndex(1).value);

linked-list/linked-list.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// list node ---> elements/items
2+
3+
const { ListNode } = require("./list-node");
4+
5+
// { value: 4, next: null }
6+
7+
// initialize --> head, size
8+
9+
// Insert/Add at particular index, begin, end
10+
// Delete at a particular index, begin, end
11+
// Get the value of node at a particular index
12+
13+
14+
function LinkedList() {
15+
this.head = new ListNode(0);
16+
this.size = 0;
17+
}
18+
19+
LinkedList.prototype.addAtIndex = function(index, value) {
20+
if (index < 0 || index > this.size) {
21+
return null;
22+
}
23+
24+
let pre = this.head;
25+
let i = 0;
26+
while (i < index) {
27+
pre = pre.next;
28+
i++;
29+
}
30+
31+
const currRef = pre.next;
32+
const newNode = new ListNode(value);
33+
pre.next = newNode;
34+
newNode.next = currRef;
35+
this.size += 1;
36+
}
37+
38+
LinkedList.prototype.addAtStart = function(value) {
39+
return this.addAtIndex(0, value);
40+
}
41+
42+
LinkedList.prototype.deleteAtIndex = function(index) {
43+
if (index < 0 || index >= this.size) {
44+
return null;
45+
}
46+
47+
let pre = this.head;
48+
let i = 0;
49+
while (i < index) {
50+
pre = pre.next;
51+
i++;
52+
}
53+
54+
pre.next = pre.next.next;
55+
this.size -= 1;
56+
}
57+
58+
LinkedList.prototype.getNodeAtIndex = function(index) {
59+
if (index < 0 || index > this.size) {
60+
return null;
61+
}
62+
63+
let pre = this.head;
64+
let i = 0;
65+
while (i <= index) {
66+
pre = pre.next;
67+
i++;
68+
}
69+
70+
return pre.value;
71+
}
72+
73+
exports.LinkedList = LinkedList
74+
/// sentinal ---> 1 ---> 2 -----> 3
75+
/// sentinal ---> 1 --> 5 --> 2 -----> 3

linked-list/list-node.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function ListNode(value) {
2+
this.value = value;
3+
this.next = null;
4+
}
5+
6+
exports.ListNode = ListNode

linked-list/test-ll.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { LinkedList } = require("./linked-list");
2+
3+
const ll = new LinkedList();
4+
5+
ll.addAtIndex(0, 15)
6+
ll.addAtIndex(0, 10)
7+
ll.addAtIndex(0, 5) // sentinal ---> 5 ---> 10 ---> 15
8+
console.log(JSON.stringify(ll.head));
9+
console.log(ll.getNodeAtIndex(2));
10+
ll.deleteAtIndex(2);
11+
console.log(JSON.stringify(ll.head));

map.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const map = new Map();
2+
3+
map.set("name", "John Doe");
4+
5+
map.set(1, true);
6+
7+
const obj = {
8+
firstname: "Dummy Name"
9+
}
10+
11+
map.set(obj, 3);
12+
13+
// console.log(map);
14+
15+
// console.log(map.get(obj));
16+
17+
map.delete(2);
18+
console.log(map);

object.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const person = {
2+
firstname: "John",
3+
lastname: "Doe",
4+
age: 21,
5+
greet: function() { return "Hello from " + this.firstname + " " + this.lastname }
6+
}
7+
console.log(person)
8+
9+
console.log(person.greet())

set.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const set = new Set();
2+
3+
set.add(1);
4+
set.add("John");
5+
6+
const obj = {
7+
firstname: "dummy"
8+
}
9+
set.add(obj);
10+
11+
set.add(1);
12+
console.log(set);
13+
console.log(set.delete(1))
14+
console.log(set);
15+

0 commit comments

Comments
 (0)