This is my assignment program in data structure to implement a QueueAsArray. I want someone to guide me about this problem cause I don’t have a strong background in java programming.
I’d like someone to give me guidance on what to do to compile and use this code in my main program.
public class QueueAsLinkedList extends AbstractContainer implements Queue
{
protected LinkedList list;
public QueueAsLinkedList ()
{ list = new LinkedList (); }
public void purge ()
{
list.purge ();
count = 0;
}
public Object getHead ()
{
if (count == 0)
throw new ContainerEmptyException ();
return list.getFirst ();
}
public void enqueue (Object object)
{
list.append (object);
++count;
}
public Object dequeue ()
{
if (count == 0)
throw new ContainerEmptyException ();
Object result = list.getFirst ();
list.extract (result);
--count;
return result;
}
public Enumeration getEnumeration()
{
return new Enumeration()
{
protected LinkedList.Element position = list.getHead();
public boolean hasMoreElements()
{
return position != null;
}
public Object nextElement()
{
if (position == null)
throw new NoSuchElementException();
Object result = position.getDatum();
position = position.getNext();
return result;
}
};
}
protected int compareTo (Comparable object)
{
AbstractContainer arg = (AbstractContainer) object;
long diff = (long) getCount() - (long) arg.getCount();
if (diff < 0)
return -1;
else if (diff > 0)
return +1;
else
return 0;
}
public boolean equals(Object object) {
LinkedList list_object = (LinkedList)object;
if(list_object.length != this.length) {
return false;
}
Element ptr = this.head;
Element list_object_ptr = list_object.head;
for(int i = 0; i < this.length; i++) {
if(list_object_ptr.getDatum () != ptr.getDatum ()) {
return false;
}
ptr = ptr.getNext ();
list_object_ptr = list_object_ptr.getNext ();
}
return true;
}
}
My suggestion to you is to read the existing LinkedList libraries source something you can easily find. I suggest you also read the source for ArrayList since you will be wrapping an array. Finally have a look at ArrayBlockingQueue because this is a Queue which wraps an array. This last class is the closest to what you want, but is the most complicated as it is concurrent and thread safe.
When you start writing a class, I suggest you start with something really simple and get that to compile. Using an IDE, it will show you whether the code will compile as you type the code and suggest corrections.
Then I would write a very simple unit test to test your very simple code. You can do this with just one method. (Some people suggest writing the test case first but I find this very hard unless you have written this sort of class before in which case you are not really writing the unit test first, just the first time for that code base)
Then add a second or third method and tests for those.
When it does something you don’t understand, use your debugger to step through the code to see what each line does.
I would use an IDE such as Netbeans, Eclipse or IntelliJ CE. I prefer IntelliJ but Netbeans is perhaps the best for a beginner.
BTW Did you write the class or was it given to you because it has more than a few unusual coding choices.
Comments on the code
LinkedListandQueueare not the standard LinkedList and Queue which is confusing.countwhich is not defined instead of the LinkedList.size().this.headandthis.lengthwhich are not fields.