Skip to content

Commit 4c65530

Browse files
authored
refactor: StackArrayList (#5356)
1 parent a03353d commit 4c65530

File tree

2 files changed

+96
-82
lines changed

2 files changed

+96
-82
lines changed

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

+20-82
Original file line numberDiff line numberDiff line change
@@ -6,110 +6,48 @@
66
/**
77
* This class implements a Stack using an ArrayList.
88
*
9-
* <p>
10-
* A stack is exactly what it sounds like. An element gets added to the top of
11-
* the stack and only the element on the top may be removed.
12-
*
13-
* <p>
14-
* This is an ArrayList Implementation of a stack, where size is not a problem
15-
* we can extend the stack as much as we want.
9+
* @param <T> the type of elements in this stack
1610
*/
17-
public class StackArrayList {
18-
19-
/**
20-
* Driver Code
21-
*/
22-
public static void main(String[] args) {
23-
StackArrayList stack = new StackArrayList();
24-
assert stack.isEmpty();
25-
26-
for (int i = 1; i <= 5; ++i) {
27-
stack.push(i);
28-
assert stack.size() == i;
29-
}
30-
31-
assert stack.size() == 5;
32-
assert stack.peek() == 5 && stack.pop() == 5 && stack.peek() == 4;
11+
public class StackArrayList<T> implements Stack<T> {
3312

34-
/* pop elements at the top of this stack one by one */
35-
while (!stack.isEmpty()) {
36-
stack.pop();
37-
}
38-
assert stack.isEmpty();
39-
40-
try {
41-
stack.pop();
42-
assert false;
43-
/* this should not happen */
44-
} catch (EmptyStackException e) {
45-
assert true;
46-
/* this should happen */
47-
}
48-
}
13+
private final ArrayList<T> stack;
4914

50-
/**
51-
* ArrayList representation of the stack
52-
*/
53-
private ArrayList<Integer> stack;
54-
55-
/**
56-
* Constructor
57-
*/
5815
public StackArrayList() {
5916
stack = new ArrayList<>();
6017
}
6118

62-
/**
63-
* Adds value to the end of list which is the top for stack
64-
*
65-
* @param value value to be added
66-
*/
67-
public void push(int value) {
19+
@Override
20+
public void push(T value) {
6821
stack.add(value);
6922
}
7023

71-
/**
72-
* Removes the element at the top of this stack and returns
73-
*
74-
* @return Element popped
75-
* @throws EmptyStackException if the stack is empty.
76-
*/
77-
public int pop() {
24+
@Override
25+
public T pop() {
7826
if (isEmpty()) {
7927
throw new EmptyStackException();
8028
}
29+
return stack.removeLast();
30+
}
8131

82-
/* remove the element on the top of the stack */
83-
return stack.remove(stack.size() - 1);
32+
@Override
33+
public T peek() {
34+
if (isEmpty()) {
35+
throw new EmptyStackException();
36+
}
37+
return stack.getLast();
8438
}
8539

86-
/**
87-
* Test if the stack is empty.
88-
*
89-
* @return {@code true} if this stack is empty, {@code false} otherwise.
90-
*/
40+
@Override
9141
public boolean isEmpty() {
9242
return stack.isEmpty();
9343
}
9444

95-
/**
96-
* Return the element at the top of this stack without removing it from the
97-
* stack.
98-
*
99-
* @return the element at the top of this stack.
100-
*/
101-
public int peek() {
102-
if (isEmpty()) {
103-
throw new EmptyStackException();
104-
}
105-
return stack.get(stack.size() - 1);
45+
@Override
46+
public void makeEmpty() {
47+
stack.clear();
10648
}
10749

108-
/**
109-
* Return size of this stack.
110-
*
111-
* @return size of this stack.
112-
*/
50+
@Override
11351
public int size() {
11452
return stack.size();
11553
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.thealgorithms.datastructures.stacks;
2+
3+
import java.util.EmptyStackException;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
class StackArrayListTest {
9+
10+
private StackArrayList<Integer> stack;
11+
12+
@BeforeEach
13+
void setUp() {
14+
stack = new StackArrayList<>();
15+
}
16+
17+
@Test
18+
void testPushAndPop() {
19+
stack.push(1);
20+
stack.push(2);
21+
stack.push(3);
22+
23+
Assertions.assertEquals(3, stack.pop());
24+
Assertions.assertEquals(2, stack.pop());
25+
Assertions.assertEquals(1, stack.pop());
26+
}
27+
28+
@Test
29+
void testPeek() {
30+
stack.push(10);
31+
stack.push(20);
32+
33+
Assertions.assertEquals(20, stack.peek());
34+
stack.pop(); // Remove 20
35+
Assertions.assertEquals(10, stack.peek());
36+
}
37+
38+
@Test
39+
void testIsEmpty() {
40+
Assertions.assertTrue(stack.isEmpty());
41+
stack.push(1);
42+
Assertions.assertFalse(stack.isEmpty());
43+
stack.pop();
44+
Assertions.assertTrue(stack.isEmpty());
45+
}
46+
47+
@Test
48+
void testMakeEmpty() {
49+
stack.push(1);
50+
stack.push(2);
51+
stack.push(3);
52+
stack.makeEmpty();
53+
Assertions.assertTrue(stack.isEmpty());
54+
Assertions.assertEquals(0, stack.size());
55+
}
56+
57+
@Test
58+
void testSize() {
59+
Assertions.assertEquals(0, stack.size());
60+
stack.push(1);
61+
stack.push(2);
62+
Assertions.assertEquals(2, stack.size());
63+
stack.pop();
64+
Assertions.assertEquals(1, stack.size());
65+
}
66+
67+
@Test
68+
void testPopEmptyStackThrowsException() {
69+
Assertions.assertThrows(EmptyStackException.class, stack::pop);
70+
}
71+
72+
@Test
73+
void testPeekEmptyStackThrowsException() {
74+
Assertions.assertThrows(EmptyStackException.class, stack::peek);
75+
}
76+
}

0 commit comments

Comments
 (0)