Skip to content

Commit 5f22570

Browse files
author
asri71
committed
Adding Stack implementation along with test class
1 parent 822c91c commit 5f22570

File tree

2 files changed

+267
-0
lines changed

2 files changed

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