Skip to content

Commit 2083d68

Browse files
authored
Enhance docs, add tests in StackArrayList (#6020)
1 parent 6545688 commit 2083d68

File tree

2 files changed

+83
-15
lines changed

2 files changed

+83
-15
lines changed

src/main/java/com/thealgorithms/datastructures/stacks/StackArrayList.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,41 @@
44
import java.util.EmptyStackException;
55

66
/**
7-
* This class implements a Stack using an ArrayList.
7+
* A stack implementation backed by an {@link ArrayList}, offering dynamic resizing
8+
* and LIFO (Last-In-First-Out) behavior.
89
*
9-
* @param <T> the type of elements in this stack
10+
* <p>The stack grows dynamically as elements are added, and elements are removed
11+
* in reverse order of their addition.
12+
*
13+
* @param <T> the type of elements stored in this stack
1014
*/
1115
public class StackArrayList<T> implements Stack<T> {
1216

1317
private final ArrayList<T> stack;
1418

19+
/**
20+
* Constructs an empty stack.
21+
*/
1522
public StackArrayList() {
1623
stack = new ArrayList<>();
1724
}
1825

26+
/**
27+
* Adds an element to the top of the stack.
28+
*
29+
* @param value the element to be added
30+
*/
1931
@Override
2032
public void push(T value) {
2133
stack.add(value);
2234
}
2335

36+
/**
37+
* Removes and returns the element from the top of the stack.
38+
*
39+
* @return the element removed from the top of the stack
40+
* @throws EmptyStackException if the stack is empty
41+
*/
2442
@Override
2543
public T pop() {
2644
if (isEmpty()) {
@@ -29,6 +47,12 @@ public T pop() {
2947
return stack.removeLast();
3048
}
3149

50+
/**
51+
* Returns the element at the top of the stack without removing it.
52+
*
53+
* @return the top element of the stack
54+
* @throws EmptyStackException if the stack is empty
55+
*/
3256
@Override
3357
public T peek() {
3458
if (isEmpty()) {
@@ -37,16 +61,29 @@ public T peek() {
3761
return stack.getLast();
3862
}
3963

64+
/**
65+
* Checks if the stack is empty.
66+
*
67+
* @return {@code true} if the stack is empty, {@code false} otherwise
68+
*/
4069
@Override
4170
public boolean isEmpty() {
4271
return stack.isEmpty();
4372
}
4473

74+
/**
75+
* Empties the stack, removing all elements.
76+
*/
4577
@Override
4678
public void makeEmpty() {
4779
stack.clear();
4880
}
4981

82+
/**
83+
* Returns the number of elements in the stack.
84+
*
85+
* @return the current size of the stack
86+
*/
5087
@Override
5188
public int size() {
5289
return stack.size();

src/test/java/com/thealgorithms/datastructures/stacks/StackArrayListTest.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ void testPeek() {
3030
stack.push(10);
3131
stack.push(20);
3232

33-
Assertions.assertEquals(20, stack.peek());
34-
stack.pop(); // Remove 20
35-
Assertions.assertEquals(10, stack.peek());
33+
Assertions.assertEquals(20, stack.peek()); // Peek should return the top element
34+
stack.pop(); // Remove top element
35+
Assertions.assertEquals(10, stack.peek()); // Peek should now return the new top element
3636
}
3737

3838
@Test
3939
void testIsEmpty() {
40-
Assertions.assertTrue(stack.isEmpty());
40+
Assertions.assertTrue(stack.isEmpty()); // Stack should initially be empty
4141
stack.push(1);
42-
Assertions.assertFalse(stack.isEmpty());
42+
Assertions.assertFalse(stack.isEmpty()); // After pushing, stack should not be empty
4343
stack.pop();
44-
Assertions.assertTrue(stack.isEmpty());
44+
Assertions.assertTrue(stack.isEmpty()); // After popping, stack should be empty again
4545
}
4646

4747
@Test
@@ -50,27 +50,58 @@ void testMakeEmpty() {
5050
stack.push(2);
5151
stack.push(3);
5252
stack.makeEmpty();
53-
Assertions.assertTrue(stack.isEmpty());
54-
Assertions.assertEquals(0, stack.size());
53+
Assertions.assertTrue(stack.isEmpty()); // Stack should be empty after makeEmpty is called
54+
Assertions.assertEquals(0, stack.size()); // Size should be 0 after makeEmpty
5555
}
5656

5757
@Test
5858
void testSize() {
59-
Assertions.assertEquals(0, stack.size());
59+
Assertions.assertEquals(0, stack.size()); // Initial size should be 0
6060
stack.push(1);
6161
stack.push(2);
62-
Assertions.assertEquals(2, stack.size());
62+
Assertions.assertEquals(2, stack.size()); // Size should reflect number of elements added
6363
stack.pop();
64-
Assertions.assertEquals(1, stack.size());
64+
Assertions.assertEquals(1, stack.size()); // Size should decrease with elements removed
6565
}
6666

6767
@Test
6868
void testPopEmptyStackThrowsException() {
69-
Assertions.assertThrows(EmptyStackException.class, stack::pop);
69+
Assertions.assertThrows(EmptyStackException.class, stack::pop); // Popping from an empty stack should throw an exception
7070
}
7171

7272
@Test
7373
void testPeekEmptyStackThrowsException() {
74-
Assertions.assertThrows(EmptyStackException.class, stack::peek);
74+
Assertions.assertThrows(EmptyStackException.class, stack::peek); // Peeking into an empty stack should throw an exception
75+
}
76+
77+
@Test
78+
void testMixedOperations() {
79+
// Testing a mix of push, pop, peek, and size operations in sequence
80+
stack.push(5);
81+
stack.push(10);
82+
stack.push(15);
83+
84+
Assertions.assertEquals(3, stack.size()); // Size should reflect number of elements
85+
Assertions.assertEquals(15, stack.peek()); // Peek should show last element added
86+
87+
stack.pop(); // Remove top element
88+
Assertions.assertEquals(10, stack.peek()); // New top should be 10
89+
Assertions.assertEquals(2, stack.size()); // Size should reflect removal
90+
91+
stack.push(20); // Add a new element
92+
Assertions.assertEquals(20, stack.peek()); // Top should be the last added element
93+
}
94+
95+
@Test
96+
void testMultipleMakeEmptyCalls() {
97+
// Ensures calling makeEmpty multiple times does not throw errors or misbehave
98+
stack.push(1);
99+
stack.push(2);
100+
stack.makeEmpty();
101+
Assertions.assertTrue(stack.isEmpty());
102+
103+
stack.makeEmpty();
104+
Assertions.assertTrue(stack.isEmpty());
105+
Assertions.assertEquals(0, stack.size());
75106
}
76107
}

0 commit comments

Comments
 (0)