Browse Source

Created the stack data structure and made junit tests for it.

master
jrtechs 6 years ago
parent
commit
3cbe62622a
4 changed files with 174 additions and 0 deletions
  1. +18
    -0
      src/main/java/net/jrtechs/www/DataStructures/Lists/Stack/IStack.java
  2. +80
    -0
      src/main/java/net/jrtechs/www/DataStructures/Lists/Stack/Stack.java
  3. +1
    -0
      src/main/java/net/jrtechs/www/DataStructures/Node.java
  4. +75
    -0
      src/test/java/net/jrtechs/www/DataStructures/Lists/StackTest.java

+ 18
- 0
src/main/java/net/jrtechs/www/DataStructures/Lists/Stack/IStack.java View File

@ -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);
}

+ 80
- 0
src/main/java/net/jrtechs/www/DataStructures/Lists/Stack/Stack.java View File

@ -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);
}
}

+ 1
- 0
src/main/java/net/jrtechs/www/DataStructures/Node.java View File

@ -6,6 +6,7 @@ package net.jrtechs.www.DataStructures;
/**
* Class used for storing data in linked structures
*
* @param <E>
*/

+ 75
- 0
src/test/java/net/jrtechs/www/DataStructures/Lists/StackTest.java View File

@ -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());
}
}

Loading…
Cancel
Save