File tree Expand file tree Collapse file tree 5 files changed +232
-0
lines changed Expand file tree Collapse file tree 5 files changed +232
-0
lines changed Original file line number Diff line number Diff line change
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 ) ;
Original file line number Diff line number Diff line change
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 ) ;
Original file line number Diff line number Diff line change
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 ( ) ) ;
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments