Skip to content

Commit 6a58c48

Browse files
committed
update DSA doubly linkedlist, queue, stack
1 parent 4f45efe commit 6a58c48

File tree

5 files changed

+289
-0
lines changed

5 files changed

+289
-0
lines changed

src/data_structure/linkedList/doublyLinkedList.test.ts

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
class DoublyLinkedList {
2+
head: any;
3+
tail: any;
4+
length: number;
5+
6+
constructor(value: any) {
7+
this.head = {
8+
value: value,
9+
prev: null,
10+
next: null,
11+
};
12+
this.tail = this.head;
13+
this.length = 1;
14+
}
15+
append(value: any) {
16+
const newNode = {
17+
value: value,
18+
prev: null,
19+
next: null,
20+
};
21+
newNode.prev = this.tail;
22+
this.tail.next = newNode;
23+
this.tail = newNode;
24+
this.length++;
25+
return this;
26+
}
27+
prepend(value: any) {
28+
const newNode = {
29+
value: value,
30+
prev: null,
31+
next: null,
32+
};
33+
newNode.next = this.head;
34+
this.head.prev = newNode;
35+
this.head = newNode;
36+
this.length++;
37+
return this;
38+
}
39+
printList() {
40+
const array = [];
41+
let currentNode = this.head;
42+
while (currentNode !== null) {
43+
array.push(currentNode.value);
44+
currentNode = currentNode.next;
45+
}
46+
return console.log(array);
47+
}
48+
insert(index: number, value: any) {
49+
//Check for proper parameters;
50+
if (index >= this.length) {
51+
console.log("yes");
52+
return this.append(value);
53+
}
54+
55+
const newNode = {
56+
value: value,
57+
prev: null,
58+
next: null,
59+
};
60+
const leader = this.traverseToIndex(index - 1);
61+
const follower = leader.next;
62+
leader.next = newNode;
63+
newNode.prev = leader;
64+
newNode.next = follower;
65+
follower.prev = newNode;
66+
this.length++;
67+
return this.printList();
68+
}
69+
traverseToIndex(index: number) {
70+
//Check parameters
71+
let counter = 0;
72+
let currentNode = this.head;
73+
while (counter !== index) {
74+
currentNode = currentNode.next;
75+
counter++;
76+
}
77+
return currentNode;
78+
}
79+
remove(index: number) {
80+
// Check Parameters
81+
const leader = this.traverseToIndex(index - 1);
82+
const unwantedNode = leader.next;
83+
leader.next = unwantedNode.next;
84+
this.length--;
85+
return this.printList();
86+
}
87+
}
88+
89+
let myLinkedList = new DoublyLinkedList(10);
90+
myLinkedList.append(5);
91+
myLinkedList.append(16);
92+
myLinkedList.prepend(1);
93+
myLinkedList.printList();
94+
myLinkedList.insert(2, 99);
95+
// myLinkedList.insert(20, 88);
96+
// myLinkedList.remove(2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Node {
2+
value: any;
3+
next: any;
4+
constructor(value: any) {
5+
this.value = value;
6+
this.next = null;
7+
}
8+
}
9+
10+
class Queue {
11+
// Dùng Single LinkedList là được rồi!
12+
first: any;
13+
last: any;
14+
length: number;
15+
constructor() {
16+
this.first = null;
17+
this.last = null;
18+
this.length = 0;
19+
}
20+
peek() {
21+
return this.first
22+
}
23+
enqueue(value: any) {
24+
if (this.length === 0) {
25+
this.first = new Node(value)
26+
this.last = this.first
27+
} else {
28+
const newNode = new Node(value)
29+
this.last.next = newNode
30+
this.last = newNode
31+
}
32+
this.length++
33+
return this.last
34+
}
35+
dequeue() {
36+
const holder = this.first
37+
this.first = this.first.next;
38+
this.length--
39+
return holder
40+
}
41+
isEmpty() {
42+
return this.length === 0
43+
}
44+
//isEmpty;
45+
}
46+
47+
const myQueue = new Queue();
48+
myQueue.enqueue('Joy');
49+
myQueue.enqueue('Matt');
50+
myQueue.enqueue('Pavel');
51+
myQueue.enqueue('Samir');
52+
// console.log("myQueue", myQueue);
53+
// myQueue.dequeue();
54+
// myQueue.dequeue();
55+
console.log("dequeue", myQueue.dequeue());
56+
// console.log("myQueue after dequeue", myQueue);
57+
58+
//Joy
59+
//Matt
60+
//Pavel
61+
//Samir
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class Node {
2+
value: any;
3+
next: any;
4+
constructor(value: any) {
5+
this.value = value;
6+
this.next = null;
7+
}
8+
}
9+
10+
class Stack {
11+
array: any = []
12+
top: any;
13+
bottom: any;
14+
length: number;
15+
constructor() {
16+
this.top = null;
17+
this.bottom = null;
18+
this.length = 0;
19+
}
20+
21+
peek() {
22+
if (this.length == 0) return null
23+
return this.top
24+
}
25+
26+
push(value: any) {
27+
this.array.push(value)
28+
if (this.length === 0) {
29+
this.top = this.array[0]
30+
this.bottom = this.array[0]
31+
} else {
32+
this.top = this.array[this.length]
33+
}
34+
this.length++
35+
36+
}
37+
38+
pop() {
39+
if (this.length == 0) return null
40+
const remove = this.array.pop()
41+
if (this.length == 0) {
42+
this.bottom = null
43+
this.top = null
44+
} else {
45+
this.top = this.array[this.array.length - 1]
46+
}
47+
this.length--
48+
return remove
49+
}
50+
51+
isEmpty() {
52+
return this.length === 0
53+
}
54+
55+
//isEmpty
56+
}
57+
58+
const myStack = new Stack();
59+
myStack.push(1)
60+
myStack.push(30)
61+
myStack.push(1000)
62+
console.log("peek", myStack.peek());
63+
console.log("pop", myStack.pop());
64+
console.log("myStack", myStack);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { LinkedList } from "../linkedList/reverseLinkedList";
2+
3+
class Node {
4+
value: any
5+
next: any
6+
constructor(value: any) {
7+
this.value = value;
8+
this.next = null;
9+
}
10+
}
11+
12+
class Stack {
13+
top: any
14+
bottom: any
15+
length: number
16+
constructor() {
17+
this.top = null;
18+
this.bottom = null;
19+
this.length = 0;
20+
}
21+
22+
peek() {
23+
return this.top
24+
}
25+
26+
push(value: any) {
27+
const newNode = new Node(value)
28+
if (this.length == 0) {
29+
this.top = newNode
30+
this.bottom = newNode
31+
} else {
32+
const holdingPointer = this.top
33+
this.top = newNode
34+
this.top.next = holdingPointer
35+
}
36+
this.length++
37+
return this
38+
}
39+
40+
pop() {
41+
if (!this.top) return null
42+
const holderPointer = this.top
43+
44+
this.top = this.top.next
45+
if (this.top == null) {
46+
this.bottom = null
47+
}
48+
this.length--
49+
return holderPointer
50+
}
51+
52+
isEmpty() {
53+
return this.length === 0
54+
}
55+
56+
//isEmpty
57+
}
58+
59+
const myStack = new Stack();
60+
myStack.push(1)
61+
myStack.push(3)
62+
myStack.push(100)
63+
myStack.push(3000)
64+
console.log("myStack", myStack);
65+
console.log("myStack", myStack.pop());
66+
// console.log("myStack.pop()", myStack.pop());
67+
// console.log("myStack", myStack);
68+
// console.log("peek", myStack.peek());

0 commit comments

Comments
 (0)