Skip to content

Commit 1093a49

Browse files
authored
Merge pull request TheAlgorithms#717 from AnkitAtBrillio/local
Adding Stack implementation along with test class
2 parents 822c91c + 46df09a commit 1093a49

File tree

2 files changed

+253
-0
lines changed

2 files changed

+253
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package src.main.java.com.dataStructures;
2+
3+
import java.io.Serializable;
4+
import java.util.EmptyStackException;
5+
6+
public class Stack<E> implements Serializable {
7+
8+
/**
9+
* Initial capacity allocated to stack on object creation
10+
*/
11+
private final int INITIAL_CAPACITY = 10;
12+
13+
/**
14+
* Increment in memory space once stack is out of space
15+
*/
16+
private final int EXTENDED_CAPACITY = 10;
17+
18+
19+
/**
20+
* Position of tail in stack
21+
*/
22+
23+
private int tail = -1;
24+
25+
/**
26+
* Size of stack at any given time
27+
*/
28+
29+
private int size;
30+
31+
/**
32+
* Uninitialized array to hold stack elements.
33+
* WIll be initialized with initial capacity once the object is created
34+
*/
35+
private Object[] elements;
36+
37+
/**
38+
* No argument to create stack object with initial capacity
39+
*/
40+
public Stack() {
41+
elements = new Object[INITIAL_CAPACITY];
42+
}
43+
44+
/**
45+
* Method to check if the given stack is empty or not
46+
*/
47+
48+
public boolean empty() {
49+
return elements == null || size == 0;
50+
}
51+
52+
53+
/**
54+
* Method to check the element on head without removing it
55+
*/
56+
57+
public Object peek() {
58+
if (empty()) {
59+
throw new EmptyStackException();
60+
}
61+
62+
return elements[tail];
63+
}
64+
65+
/**
66+
* Method to remove the top element from stack
67+
*/
68+
69+
public Object pop() {
70+
if (empty()) {
71+
throw new EmptyStackException();
72+
}
73+
74+
Object removedElement = elements[tail];
75+
tail--;
76+
size--;
77+
return removedElement;
78+
}
79+
80+
/**
81+
* Method to add element to stack
82+
*/
83+
public Object push(Object e) {
84+
85+
boolean isSuccess = false;
86+
if (tail < (INITIAL_CAPACITY - 1)) {
87+
tail++;
88+
elements[tail] = e;
89+
} else {
90+
Object[] extendedElements = new Object[INITIAL_CAPACITY + EXTENDED_CAPACITY];
91+
System.arraycopy(elements, 0, extendedElements, 0, (tail + 1));
92+
elements = extendedElements;
93+
tail++;
94+
elements[tail] = e;
95+
}
96+
size++;
97+
return e;
98+
99+
}
100+
101+
/**
102+
* Method to search for an element in stack
103+
*/
104+
105+
public int search(Object o) {
106+
107+
int index = -1;
108+
boolean found = false;
109+
if (empty()) {
110+
return -1;
111+
}
112+
113+
for (int i = 0; i < size(); i++) {
114+
if (elements[i] == o) {
115+
index = i;
116+
found = true;
117+
break;
118+
}
119+
}
120+
121+
if (found) {
122+
index = tail - index + 1;
123+
}
124+
125+
return index;
126+
}
127+
128+
/**
129+
* Method to get size of stack
130+
*/
131+
public int size() {
132+
return size;
133+
}
134+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package src.test.java.com.dataStructures;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import src.main.java.com.dataStructures.Stack;
6+
7+
import java.util.EmptyStackException;
8+
9+
public class StackTest {
10+
11+
@Test
12+
public void testEmpty() {
13+
14+
Stack<Integer> myStack = new Stack<>();
15+
boolean isEmpty = myStack.empty();
16+
Assert.assertTrue(isEmpty);
17+
18+
myStack.push(10);
19+
isEmpty = myStack.empty();
20+
Assert.assertFalse(isEmpty);
21+
}
22+
23+
@Test(expected = EmptyStackException.class)
24+
public void testPeekWithoutElements() {
25+
26+
Stack<Integer> myStack = new Stack<>();
27+
myStack.peek();
28+
}
29+
30+
@Test
31+
public void testPeekWithElements() {
32+
33+
Stack<Integer> myStack = new Stack<>();
34+
myStack.push(10);
35+
myStack.push(20);
36+
myStack.push(30);
37+
myStack.push(40);
38+
39+
Assert.assertEquals(40, myStack.peek());
40+
}
41+
42+
@Test(expected = EmptyStackException.class)
43+
public void testPopWithoutElements() {
44+
45+
Stack<Integer> myStack = new Stack<>();
46+
myStack.pop();
47+
48+
}
49+
50+
@Test
51+
public void testPopWithElements() {
52+
53+
Stack<Integer> myStack = new Stack<>();
54+
myStack.push(10);
55+
myStack.push(20);
56+
myStack.push(30);
57+
myStack.push(40);
58+
myStack.push(50);
59+
60+
Assert.assertEquals(50, myStack.pop());
61+
62+
}
63+
64+
@Test
65+
public void testPushWithinInitialCapacity() {
66+
67+
Stack<Integer> myStack = new Stack<>();
68+
myStack.push(10);
69+
myStack.push(20);
70+
myStack.push(30);
71+
myStack.push(40);
72+
myStack.push(50);
73+
myStack.push(60);
74+
myStack.push(70);
75+
myStack.push(80);
76+
myStack.push(90);
77+
myStack.push(100);
78+
Assert.assertEquals(10, myStack.size());
79+
}
80+
81+
@Test
82+
public void testPushOutsideInitialCapacity() {
83+
84+
Stack<Integer> myStack = new Stack<>();
85+
myStack.push(10);
86+
myStack.push(20);
87+
myStack.push(30);
88+
myStack.push(40);
89+
myStack.push(50);
90+
myStack.push(60);
91+
myStack.push(70);
92+
myStack.push(80);
93+
myStack.push(90);
94+
myStack.push(100);
95+
myStack.push(110);
96+
Assert.assertEquals(11, myStack.size());
97+
}
98+
99+
@Test
100+
public void testSearchWithObjectUnavailable() {
101+
102+
Stack<Integer> myStack = new Stack<>();
103+
myStack.push(10);
104+
myStack.push(20);
105+
myStack.push(30);
106+
Assert.assertEquals(-1,myStack.search(50));
107+
}
108+
109+
@Test
110+
public void testSearchWithObjectAvailable() {
111+
112+
Stack<Integer> myStack = new Stack<>();
113+
myStack.push(10);
114+
myStack.push(20);
115+
myStack.push(30);
116+
Assert.assertEquals(3,myStack.search(10));
117+
118+
}
119+
}

0 commit comments

Comments
 (0)