Ok, well I don't actually cover this until my next Data Structures lecture (I think). But I'll try my best to put pen to paper (or finger to keyboard
).
Code:
import java.util.*;
public class Queue {
private Vector queue;
public Queue() {
queue = new Vector();
}
public int size() {
return queue.size();
}
public void add(Object o) {
queue.add(o);
}
public Object remove() {
String element = (String) queue.firstElement();
queue.remove(element);
return element;
}
}
I've briefly test the code by shoving the following in at the end before closing the class:
Code:
public static void main (String[] args) {
Queue bob = new Queue();
bob.add("Hello Big ");
bob.add("Fat ");
bob.add("Man");
System.out.println("Queue is " + bob.size() + " and message is: " + (String) bob.remove() + (String) bob.remove() + (String) bob.remove());
}
It seems to work fine. There's extra features you can add but all I've done is what you asked, so if you need anything more you'll have to ask. Let me know how that is for you.
Bookmarks