Skip to content

Commit 01cf60f

Browse files
committed
Stack & Queue Complete Course
1 parent 7b13775 commit 01cf60f

File tree

5 files changed

+232
-0
lines changed

5 files changed

+232
-0
lines changed

5. Stack & Queue/1. Stack/index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Node {
2+
constructor(value) {
3+
this.value = value;
4+
this.next = null;
5+
}
6+
}
7+
8+
class Stack {
9+
constructor(value) {
10+
const newNode = new Node(value);
11+
this.first = newNode;
12+
this.length = 1;
13+
}
14+
15+
push(value) {
16+
const newNode = new Node(value);
17+
18+
if (this.length === 0) {
19+
this.first = newNode;
20+
} else {
21+
newNode.next = this.first;
22+
this.first = newNode;
23+
this.length++;
24+
return this;
25+
}
26+
}
27+
28+
pop() {
29+
if (this.length === 0) {
30+
return undefined;
31+
}
32+
33+
let temp = this.first;
34+
this.first = this.first.next;
35+
temp.next = null;
36+
this.length--;
37+
return temp;
38+
}
39+
40+
top() {
41+
if (this.length === 0) {
42+
return undefined;
43+
}
44+
return this.first;
45+
}
46+
}
47+
48+
let theStack = new Stack(0);
49+
theStack.push(1);
50+
theStack.push(2);
51+
// theStack.pop();
52+
console.log(theStack);

5. Stack & Queue/2. Queue/index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Node {
2+
constructor(value) {
3+
this.value = value;
4+
this.next = null;
5+
}
6+
}
7+
8+
class Queue {
9+
constructor(value) {
10+
const newNode = new Node(value);
11+
this.first = newNode;
12+
this.last = newNode;
13+
this.length = 1;
14+
}
15+
16+
enqueue(value) {
17+
const newNode = new Node(value);
18+
if (this.length === 0) {
19+
this.first = newNode;
20+
this.last = newNode;
21+
}
22+
23+
this.last.next = newNode;
24+
this.last = newNode;
25+
this.length++;
26+
return this;
27+
}
28+
29+
dequeue() {
30+
if (this.length === 0) {
31+
return undefined;
32+
}
33+
34+
let temp = this.first;
35+
36+
if (this.length === 1) {
37+
this.first = null;
38+
this.last = null;
39+
}
40+
41+
this.first = this.first.next;
42+
temp.next = null;
43+
this.length--;
44+
return temp;
45+
}
46+
}
47+
48+
let myQueue = new Queue(0);
49+
myQueue.enqueue(1);
50+
myQueue.enqueue(2);
51+
myQueue.dequeue();
52+
console.log(myQueue);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class Node {
2+
constructor(value) {
3+
this.value = value;
4+
this.next = null;
5+
}
6+
}
7+
8+
class Stack {
9+
constructor(value) {
10+
const newNode = new Node(value);
11+
this.first = newNode;
12+
this.length = 1;
13+
}
14+
15+
push(value) {
16+
const newNode = new Node(value);
17+
18+
if (this.length === 0) {
19+
this.first = newNode;
20+
} else {
21+
newNode.next = this.first;
22+
this.first = newNode;
23+
this.length++;
24+
}
25+
return this;
26+
}
27+
28+
pop() {
29+
if (this.length === 0) {
30+
return undefined;
31+
}
32+
33+
let temp = this.first;
34+
this.first = this.first.next;
35+
temp.next = null;
36+
this.length--;
37+
return temp;
38+
}
39+
40+
top() {
41+
if (this.length === 0) {
42+
return undefined;
43+
}
44+
return this.first;
45+
}
46+
47+
min() {
48+
if (this.length === 0) {
49+
return undefined;
50+
}
51+
52+
let current = this.first;
53+
let minValue = current.value;
54+
55+
while (current.next) {
56+
current = current.next;
57+
if (current.value < minValue) {
58+
console.log(current.value, minValue);
59+
minValue = current.value;
60+
}
61+
}
62+
63+
return minValue;
64+
}
65+
}
66+
67+
let theStack = new Stack();
68+
theStack.push(1);
69+
theStack.push(2);
70+
theStack.push(3);
71+
console.log(theStack.min());
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const isValidParenthesis = (str) => {
2+
// Use a stack to store opening brackets
3+
const stack = [];
4+
5+
// Map opening brackets to their closing counterparts
6+
const brackets = {
7+
"{": "}",
8+
"[": "]",
9+
"(": ")",
10+
};
11+
12+
// Loop through each character in the string
13+
for (let char of str) {
14+
// If it's an opening bracket, push it to the stack
15+
if (brackets[char]) {
16+
stack.push(char);
17+
} else {
18+
// If it's a closing bracket, check if it matches the top of the stack
19+
const top = stack.pop();
20+
if (!top || brackets[top] !== char) {
21+
return false;
22+
}
23+
}
24+
}
25+
26+
// After iterating, check if the stack is empty (all brackets were matched)
27+
return stack.length === 0;
28+
};
29+
30+
console.log(isValidParenthesis("(){}[]")); // true
31+
console.log(isValidParenthesis("([)]")); // false
32+
console.log(isValidParenthesis("()")); // true
33+
console.log(isValidParenthesis("(")); // false
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function reverseString(str) {
2+
// Create an empty stack
3+
const stack = [];
4+
5+
// Push each character of the string onto the stack
6+
for (let char of str) {
7+
stack.push(char);
8+
}
9+
10+
// Initialize an empty string to store the reversed string
11+
let reversedStr = "";
12+
13+
// Pop characters from the stack and build the reversed string
14+
while (stack.length > 0) {
15+
reversedStr += stack.pop();
16+
}
17+
18+
// Return the reversed string
19+
return reversedStr;
20+
}
21+
22+
const originalString = "hello world";
23+
const reversedString = reverseString(originalString);
24+
console.log(reversedString); // Output: dlrow olleh

0 commit comments

Comments
 (0)