Last active
June 17, 2018 19:00
-
-
Save ebenezergraham/11bc23afdcde1aa21716fb81165a3dca to your computer and use it in GitHub Desktop.
Generic Abstract Data Type - Stack Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dsa.weektwo; | |
import java.util.Arrays; | |
import java.util.EmptyStackException; | |
/** | |
* | |
* @author ebenezergraham | |
* @param <T> | |
*/ | |
public class StackImpl<T extends Object> implements Stack<T> { | |
private T [] stack; | |
private int head; | |
private int capacity; | |
public StackImpl() { | |
this.head = -1; | |
this.capacity = 10; | |
stack = (T[]) new Object[capacity]; | |
} | |
@Override | |
public boolean empty() { | |
return stack.length == 0; | |
} | |
@Override | |
public T peek() throws EmptyStackException { | |
if(!empty()){ | |
return stack[head]; | |
}else{ | |
throw new EmptyStackException(); | |
} | |
} | |
@Override | |
public T pop() throws EmptyStackException { | |
if(!empty()){ | |
T element = peek(); | |
return element; | |
}else{ | |
throw new EmptyStackException(); | |
} | |
} | |
@Override | |
public boolean push(T o) { | |
if(isFull()){ | |
this.capacity = this.capacity * 2; | |
this.stack = Arrays.copyOf(stack, this.capacity); | |
} | |
stack[++head] = o; | |
return true; | |
} | |
@Override | |
public int search(T o) { | |
if(!empty()){ | |
for(int index = 0; index<stack.length; index++){ | |
if(this.stack[index].equals(o)){ | |
return index; | |
} | |
} | |
} | |
return -1; | |
} | |
@Override | |
public boolean contains(T o){ | |
return search(o) != -1; | |
} | |
public boolean isFull(){ | |
return this.stack.length == this.capacity; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package dsa.weektwo; | |
import static org.junit.Assert.*; | |
import org.junit.Test; | |
/** | |
* | |
* @author ebene | |
*/ | |
public class StackImplTest { | |
StackImpl testInstance; | |
public StackImplTest() { | |
testInstance = new StackImpl(); | |
testInstance.push(5); | |
testInstance.push(10); | |
testInstance.push(15); | |
} | |
/** | |
* Test of empty method, of class StackImpl. | |
*/ | |
@Test | |
public void testEmpty() { | |
StackImpl instance = testInstance; | |
boolean expResult = false; | |
boolean result = instance.empty(); | |
assertEquals(expResult, result); | |
} | |
/** | |
* Test of peek method, of class StackImpl. | |
*/ | |
@Test | |
public void testPeek() { | |
StackImpl instance = testInstance; | |
Object expResult = 15; | |
Object result = instance.peek(); | |
assertEquals(expResult, result); | |
} | |
/** | |
* Test of pop method, of class StackImpl. | |
*/ | |
@Test | |
public void testPop() { | |
StackImpl instance = testInstance; | |
Object expResult = 15; | |
Object result = instance.pop(); | |
assertEquals(expResult, result); | |
} | |
/** | |
* Test of push method, of class StackImpl. | |
*/ | |
@Test | |
public void testPush() { | |
Object o = 20; | |
StackImpl instance = new StackImpl(); | |
boolean expResult = true; | |
boolean result = instance.push(o); | |
assertEquals(expResult, result); | |
assertEquals(o,instance.peek()); | |
} | |
/** | |
* Test of search method, of class StackImpl. | |
*/ | |
@Test | |
public void testSearch() { | |
Object o = Integer.MAX_VALUE; | |
StackImpl instance = new StackImpl(); | |
instance.push(o); | |
int result = instance.search(o); | |
assertEquals(0, result); | |
} | |
/** | |
* Test of contains method, of class StackImpl. | |
*/ | |
@Test | |
public void testContains() { | |
Object o = 15; | |
StackImpl instance = new StackImpl(); | |
instance.push(o); | |
boolean expResult = true; | |
boolean result = instance.contains(o); | |
assertEquals(expResult, result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dsa.weektwo; | |
/** | |
* | |
* @author ebenezergraham | |
* @param <T> | |
*/ | |
public interface Stack<T> { | |
public boolean empty(); | |
public T peek(); | |
public T pop(); | |
public boolean push(T t ); | |
public int search(T o); | |
public boolean contains(T o); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@stephennaicken thanks for the feedback. I have refactored my code. making a copy of the array everytime is a really horrible idea. 😆