Created
September 11, 2008 09:58
-
-
Save ishikawa/10201 to your computer and use it in GitHub Desktop.
Queue
This file contains hidden or 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
| /** | |
| * http://en.wikipedia.org/wiki/Queue_(data_structure) | |
| */ | |
| public class Queue <E> { | |
| private LinkedList<E> elements = new LinkedList<E>(); | |
| public int size() { return elements.size(); } | |
| public boolean isEmpty() { return elements.isEmpty(); } | |
| public E front() { return elements.get(0); } | |
| public E pop() { return elements.removeFirst(); } | |
| public void push(E element) { elements.add(element); } | |
| } | |
This file contains hidden or 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 org.metareal; | |
| import junit.framework.TestCase; | |
| public class QueueTest extends TestCase { | |
| public void test_empty() { | |
| Queue<?> queue = new Queue<Object>(); | |
| assertNotNull(queue); | |
| assertTrue(queue.isEmpty()); | |
| assertEquals(0, queue.size()); | |
| } | |
| public void test_simple() { | |
| Queue<Integer> queue = new Queue<Integer>(); | |
| queue.push(1); | |
| queue.push(2); | |
| queue.push(3); | |
| assertFalse(queue.isEmpty()); | |
| assertEquals(3, queue.size()); | |
| assertEquals(new Integer(1), queue.front()); | |
| assertEquals(3, queue.size()); | |
| assertEquals(new Integer(1), queue.pop()); | |
| assertEquals(2, queue.size()); | |
| assertEquals(new Integer(2), queue.pop()); | |
| assertEquals(1, queue.size()); | |
| assertEquals(new Integer(3), queue.pop()); | |
| assertEquals(0, queue.size()); | |
| } | |
| public void test_push_pop() { | |
| Queue<Integer> queue = new Queue<Integer>(); | |
| queue.push(0); | |
| assertFalse(queue.isEmpty()); | |
| assertEquals(1, queue.size()); | |
| assertEquals(new Integer(0), queue.pop()); | |
| assertTrue(queue.isEmpty()); | |
| assertEquals(0, queue.size()); | |
| queue.push(1); | |
| queue.push(2); | |
| assertFalse(queue.isEmpty()); | |
| assertEquals(2, queue.size()); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment