@ -0,0 +1,18 @@ | |||
package net.jrtechs.www.DataStructures.Lists.Stack; | |||
/** | |||
* Interface for defining behavior of a Stack | |||
* | |||
* @author Jeffery Russell 8-25-18 | |||
* @param <E> | |||
*/ | |||
public interface IStack<E> | |||
{ | |||
public boolean empty(); | |||
public E peek(); | |||
public E pop(); | |||
public void push(E item); | |||
} |
@ -0,0 +1,80 @@ | |||
package net.jrtechs.www.DataStructures.Lists.Stack; | |||
import net.jrtechs.www.DataStructures.Node; | |||
/** | |||
* @author Jeffery Russell 8-25-18 | |||
* @param <E> generic stored in collection | |||
*/ | |||
public class Stack<E> implements IStack<E> | |||
{ | |||
/** Head node */ | |||
private Node<E> head; | |||
public Stack() | |||
{ | |||
this.head = null; | |||
} | |||
public Stack(E o) | |||
{ | |||
this.head = new Node(o); | |||
} | |||
/** | |||
* Determines if the stack is empty | |||
* | |||
* @return | |||
*/ | |||
@Override | |||
public boolean empty() | |||
{ | |||
return (this.head == null); | |||
} | |||
/** | |||
* Retrieves top element with out removing it | |||
* | |||
* @return top element | |||
*/ | |||
@Override | |||
public E peek() | |||
{ | |||
if(this.head == null) | |||
return null; | |||
return head.getData(); | |||
} | |||
/** | |||
* Returns and removes top element from | |||
* the stack. | |||
* | |||
* @return top element | |||
*/ | |||
@Override | |||
public E pop() | |||
{ | |||
if(this.empty()) | |||
{ | |||
return null; | |||
} | |||
E temp = this.head.getData(); | |||
this.head = this.head.getNext(); | |||
return temp; | |||
} | |||
/** | |||
* Adds a new element to the Stack. | |||
* | |||
* @param item to add to stack | |||
*/ | |||
@Override | |||
public void push(E item) | |||
{ | |||
this.head = new Node<>(item, this.head); | |||
} | |||
} |
@ -0,0 +1,75 @@ | |||
package net.jrtechs.www.DataStructures.Lists; | |||
import net.jrtechs.www.DataStructures.Lists.Stack.Stack; | |||
import org.junit.Test; | |||
import static junit.framework.TestCase.assertFalse; | |||
import static junit.framework.TestCase.assertNotNull; | |||
import static junit.framework.TestCase.assertTrue; | |||
public class StackTest | |||
{ | |||
@Test | |||
public void testCreation() | |||
{ | |||
assertNotNull(new Stack<>(12.0)); | |||
assertNotNull(new Stack<>(14.0)); | |||
} | |||
@Test | |||
public void testInsertion() | |||
{ | |||
Stack<Double> stack = new Stack<>(); | |||
stack.push(13.0); | |||
stack.push(23.0); | |||
} | |||
@Test | |||
public void testPeek() | |||
{ | |||
Stack<Double> stack = new Stack<>(); | |||
stack.push(13.0); | |||
stack.push(23.0); | |||
assertTrue(stack.peek() == 23.0); | |||
} | |||
@Test | |||
public void testPop() | |||
{ | |||
Stack<Double> stack = new Stack<>(); | |||
stack.push(13.0); | |||
stack.push(23.0); | |||
assertTrue(stack.pop() == 23.0); | |||
assertTrue(stack.pop() == 13.0); | |||
} | |||
@Test | |||
public void testEmpty() | |||
{ | |||
Stack<Double> stack = new Stack<>(); | |||
assertTrue(stack.empty()); | |||
Stack<Double> stack22 = new Stack<>(234.0); | |||
assertFalse(stack22.empty()); | |||
stack.push(13.0); | |||
stack.push(23.0); | |||
assertTrue(stack.pop() == 23.0); | |||
assertTrue(stack.pop() == 13.0); | |||
assertTrue(stack.empty()); | |||
} | |||
} |