you're on the right track with using a LinkedList
, this is one of the easiest ways to implement your own Queue
- I hope this is an exercise, since you should otherwise use Java's built-in Queue
.
I must confess I found your question interesting and decided to write a Queue so as to demonstrate. Note that you should create a class for your custom Queue
. If you implement the whole thing in main
, there'll be no way to reuse the code.
So you'd create a new file, let's call it Queue.java
. It would look something like this:
import java.util.*;
public class Queue<T> implements Iterable<T> {
private LinkedList<T> elements = new LinkedList<T>();
public void enqueue(T element) {
elements.add(element);
}
public T dequeue() {
return elements.removeFirst();
}
public T peek() {
return elements.getFirst();
}
public void clear() {
elements.clear();
}
public int size() {
return elements.size();
}
public boolean isEmpty() {
return elements.isEmpty();
}
@Override
public Iterator<T> iterator() {
return elements.iterator();
}
}
Notice that that the code is extremely simple - no method's implementation is longer than one line. However, I don't know your level of experience so there might be two weird things in here: the use of Generics and the implements Iterable<T>
.
The <T>
means you can put any Java object into the Queue
keeping Type safety.
This means, when you call Queue.Dequeue()
, the result will still have the same type that it had when you Enqueued
it.
Oracle says:
Implementing this interface allows an object to be the target of the foreach
statement.
Note that you don't need to know more, because we simply delegate this responsibility to LinkedList.iterator()
which does the work for us.
Below is some code to test your new Queue. Play around with it. You can add other queue tests to main
, for instance:
testQueue(true, false, true, true);
or
testQueue(0.5, 3.6, -5.4, 0.0);
to test putting different types of data into your Queue
.
If anything remains unclear, feel free to leave a comment, and I'll update my answer.
public class Main {
public static void main(String[] args) {
testQueue(1, 2, 3, 4, 5);
testQueue("first", "second", "third");
}
private static <T> void testQueue(T... elements) {
outputLine("testing a Queue<%s>", getClassOf(elements));
Queue<T> queue = new Queue<T>();
outputSize(queue);
enqueueElementsInto(queue, elements);
outputSize(queue);
loopOverAllElementsOf(queue);
dequeueAllFrom(queue);
outputSize(queue);
outputLine("---");
}
private static <T> void outputSize(Queue<T> queue) {
outputLine("empty: %s, size: %s elements", queue.isEmpty(), queue.size());
}
private static <T> void enqueueElementsInto(Queue<T> queue, T... elements) {
outputLine("enqueueing: ");
for (T element : elements) {
queue.enqueue(element);
output(element + " ");
}
}
private static <T> void loopOverAllElementsOf(Queue<T> queue) {
outputLine("Queue contains: ");
for (T element : queue) {
output(element + " ");
}
}
private static <T> void dequeueAllFrom(Queue<T> queue) {
outputLine("dequeueing: ");
while (!queue.isEmpty()) {
T next = queue.peek();
T dequeued = queue.dequeue();
outputLine("expected: %s ", next);
output("dequeued: %s ", dequeued);
}
}
private static void outputLine(String format, Object... params) {
output('\n' + format, params);
}
private static void output(String format, Object... params) {
System.out.print(String.format(format, params));
}
private static <T> String getClassOf(T... elements) {
String nameOfArray = elements.getClass().getSimpleName();
return nameOfArray.substring(0, nameOfArray.length() - 2);
}
}
output:
testing a Queue<Integer>
empty: true, size: 0 elements
enqueueing: 1 2 3 4 5
empty: false, size: 5 elements
Queue contains: 1 2 3 4 5
dequeueing:
expected: 1 dequeued: 1
expected: 2 dequeued: 2
expected: 3 dequeued: 3
expected: 4 dequeued: 4
expected: 5 dequeued: 5
empty: true, size: 0 elements
---
testing a Queue<String>
empty: true, size: 0 elements
enqueueing: first second third
empty: false, size: 3 elements
Queue contains: first second third
dequeueing:
expected: first dequeued: first
expected: second dequeued: second
expected: third dequeued: third
empty: true, size: 0 elements
---
LinkedList
and then attempt to call methods on it that don't exist. \$\endgroup\$