File tree 4 files changed +49
-6
lines changed
main/java/com/thealgorithms/stacks
test/java/com/thealgorithms/stacks
4 files changed +49
-6
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ public void push(int data) {
37
37
}
38
38
39
39
mainStack .push (data );
40
- if (data > maxStack .peek ()) {
40
+ if (! maxStack . isEmpty () && data > maxStack .peek ()) {
41
41
maxStack .push (data );
42
42
}
43
43
}
@@ -55,7 +55,7 @@ public void pop() {
55
55
}
56
56
57
57
int ele = mainStack .pop ();
58
- if (ele == maxStack .peek ()) {
58
+ if (! maxStack . isEmpty () && ele == maxStack .peek ()) {
59
59
maxStack .pop ();
60
60
}
61
61
}
Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ public void push(int data) {
35
35
}
36
36
37
37
mainStack .push (data );
38
- if (data < minStack .peek ()) {
38
+ if (! minStack . isEmpty () && data < minStack .peek ()) {
39
39
minStack .push (data );
40
40
}
41
41
}
@@ -53,7 +53,7 @@ public void pop() {
53
53
}
54
54
55
55
int ele = mainStack .pop ();
56
- if (ele == minStack .peek ()) {
56
+ if (! minStack . isEmpty () && ele == minStack .peek ()) {
57
57
minStack .pop ();
58
58
}
59
59
}
Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .stacks ;
2
2
3
- import static org .junit .jupiter .api .Assertions .assertEquals ;
4
3
import org .junit .jupiter .api .BeforeEach ;
5
4
import org .junit .jupiter .api .Test ;
6
5
6
+ import java .util .NoSuchElementException ;
7
+
8
+ import static org .junit .jupiter .api .Assertions .*;
9
+
7
10
public class GreatestElementConstantTimeTest {
8
11
9
12
private GreatestElementConstantTime gect ;
@@ -33,4 +36,22 @@ public void testMinTwo() {
33
36
gect .pop ();
34
37
assertEquals (10 , gect .getMaximumElement ());
35
38
}
39
+
40
+ @ Test
41
+ public void testNullMax () {
42
+ gect .push (10 );
43
+ gect .push (20 );
44
+ gect .pop ();
45
+ gect .pop ();
46
+ assertNull (gect .getMaximumElement ());
47
+ }
48
+
49
+ @ Test
50
+ public void testBlankHandle () {
51
+ gect .push (10 );
52
+ gect .push (1 );
53
+ gect .pop ();
54
+ gect .pop ();
55
+ assertThrows (NoSuchElementException .class , () -> gect .pop ());
56
+ }
36
57
}
Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .stacks ;
2
2
3
- import static org .junit .jupiter .api .Assertions .assertEquals ;
4
3
import org .junit .jupiter .api .BeforeEach ;
5
4
import org .junit .jupiter .api .Test ;
6
5
6
+ import java .util .NoSuchElementException ;
7
+
8
+ import static org .junit .jupiter .api .Assertions .*;
9
+
7
10
public class SmallestElementConstantTimeTest {
8
11
9
12
private SmallestElementConstantTime sect ;
@@ -32,4 +35,23 @@ public void testMinTwo() {
32
35
sect .pop ();
33
36
assertEquals (5 , sect .getMinimumElement ());
34
37
}
38
+
39
+ @ Test
40
+ public void testNullMin () {
41
+ sect .push (10 );
42
+ sect .push (20 );
43
+ sect .pop ();
44
+ sect .pop ();
45
+ assertNull (sect .getMinimumElement ());
46
+ }
47
+
48
+ @ Test
49
+ public void testBlankHandle () {
50
+ sect .push (10 );
51
+ sect .push (1 );
52
+ sect .pop ();
53
+ sect .pop ();
54
+ assertThrows (NoSuchElementException .class , () -> sect .pop ());
55
+ }
56
+
35
57
}
You can’t perform that action at this time.
0 commit comments