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