diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2017-02-06 11:41:36 -0500 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2017-02-06 11:41:36 -0500 |
| commit | 89cdf3efb49335e7c07a68a5a64657eeec2288a6 (patch) | |
| tree | cdc0fd8165e65b1637fa54cac11c932acefc8a89 /labs/lab10_tree/SimpleQueue.java | |
| download | coe0445-89cdf3efb49335e7c07a68a5a64657eeec2288a6.tar.gz coe0445-89cdf3efb49335e7c07a68a5a64657eeec2288a6.tar.bz2 coe0445-89cdf3efb49335e7c07a68a5a64657eeec2288a6.zip | |
Diffstat (limited to 'labs/lab10_tree/SimpleQueue.java')
| -rw-r--r-- | labs/lab10_tree/SimpleQueue.java | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/labs/lab10_tree/SimpleQueue.java b/labs/lab10_tree/SimpleQueue.java new file mode 100644 index 0000000..64d7c11 --- /dev/null +++ b/labs/lab10_tree/SimpleQueue.java @@ -0,0 +1,72 @@ + +public class SimpleQueue<T> +{ + private Node firstNode; + private Node lastNode; + + public SimpleQueue() + { + firstNode = null; + lastNode = null; + } + + public void enqueue(T newEntry) + { + Node newNode = new Node(newEntry); + + if(firstNode == null) + { + firstNode = newNode; + lastNode = newNode; + } + else + { + lastNode.next = newNode; + lastNode = newNode; + } + } + + public T dequeue() + { + T result = null; + + if(firstNode != null) + { + result = firstNode.data; + if(lastNode == firstNode) + { + lastNode = null; + } + firstNode = firstNode.next; + } + + return result; + } + + public T getFront() + { + if(firstNode != null) + { + return firstNode.data; + } + + return null; + } + + private class Node + { + private T data; + private Node next; + + public Node(T aData, Node nextNode) + { + data = aData; + next = nextNode; + } + + public Node(T aData) + { + this(aData, null); + } + } +} |
