Browse Source

Created and tested the queue data structure

master
jrtechs 5 years ago
parent
commit
6338430983
4 changed files with 167 additions and 0 deletions
  1. +22
    -0
      src/main/java/net/jrtechs/www/DataStructures/Lists/Queue/IQueue.java
  2. +84
    -0
      src/main/java/net/jrtechs/www/DataStructures/Lists/Queue/Queue.java
  3. +56
    -0
      src/test/java/net/jrtechs/www/DataStructures/Lists/QueueTest.java
  4. +5
    -0
      src/test/java/net/jrtechs/www/DataStructures/Lists/StackTest.java

+ 22
- 0
src/main/java/net/jrtechs/www/DataStructures/Lists/Queue/IQueue.java View File

@ -0,0 +1,22 @@
package net.jrtechs.www.DataStructures.Lists.Queue;
/**
* @author Jeffery Russell 8-26-18
*/
public interface IQueue<E>
{
/**
* Adds a new element to the end of the queue
*
* @param element object to add
*/
public void enqueue(E element);
/**
* Removes an object from the queue.
*
* @return the object removed
*/
public E dequeue();
}

+ 84
- 0
src/main/java/net/jrtechs/www/DataStructures/Lists/Queue/Queue.java View File

@ -0,0 +1,84 @@
package net.jrtechs.www.DataStructures.Lists.Queue;
import net.jrtechs.www.DataStructures.Node;
/**
* Data structure which stores objects in a linked
* list where objects inserted first are removed first,
* and where objects inserted last are removed last.
*
* @author Jeffery Russell 8-26-18
* @param <E> generic data type
*/
public class Queue<E> implements IQueue<E>
{
/** Head node */
private Node<E> head;
/**Stored tail to make insertions o(1) */
private Node<E> tail;
/**
* Creates an empty queue
*/
public Queue()
{
this.head = null;
this.tail = null;
}
/**
* Creates a queue with a single element in it
*
* @param element object to add to queue
*/
public Queue(E element)
{
this.head = new Node<>(element);
this.tail = head;
}
/**
* Adds a single element to the queue
*
* @param element object to add
*/
@Override
public void enqueue(E element)
{
Node<E> newElement = new Node<>(element);
if(this.tail == null)
{
this.head = newElement;
this.tail = newElement;
}
else
{
this.tail.setNext(newElement);
this.tail = newElement;
}
}
/**
* Removes the element from the front of the queue
*
* @return element removed
*/
@Override
public E dequeue()
{
if(this.head != null)
{
E temp = this.head.getData();
this.head = this.head.getNext();
return temp;
}
return null;
}
}

+ 56
- 0
src/test/java/net/jrtechs/www/DataStructures/Lists/QueueTest.java View File

@ -0,0 +1,56 @@
package net.jrtechs.www.DataStructures.Lists;
import net.jrtechs.www.DataStructures.Lists.Queue.Queue;
import org.junit.Test;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;
/**
* JUnit tests for
* {@link net.jrtechs.www.DataStructures.Lists.Queue.Queue}
*
* @author Jeffery Russell 8-26-18
*/
public class QueueTest
{
@Test
public void testCreation()
{
assertNotNull(new Queue<>());
assertNotNull(new Queue<>(12.0));
}
@Test
public void testInsertion()
{
Queue<String> words = new Queue<>();
words.enqueue("One");
words.enqueue("Two");
words.enqueue("Three");
assertNotNull(words.dequeue());
}
@Test
public void testDequeue()
{
Queue<String> words = new Queue<>();
words.enqueue("One");
words.enqueue("Two");
words.enqueue("Three");
assertTrue(words.dequeue().equals("One"));
assertTrue(words.dequeue().equals("Two"));
assertTrue(words.dequeue().equals("Three"));
assertNull(words.dequeue());
}
}

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

@ -7,6 +7,11 @@ import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
/**
* JUnit tests for {@link Stack}
*
* @author Jeffery Russell 8-25-18
*/
public class StackTest
{

Loading…
Cancel
Save