Skip to content

Commit cf872fb

Browse files
author
Alex Klymenko
committed
refactor: StackArrayList
1 parent f5c0314 commit cf872fb

File tree

2 files changed

+97
-82
lines changed

2 files changed

+97
-82
lines changed

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

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

0 commit comments

Comments
 (0)