这是我在数据结构中实现 QueueAsArray 的分配程序。我希望有人指导我解决这个问题,因为我在 Java 编程方面没有很强的背景。
我希望有人指导我如何在我的主程序中编译和使用此代码。
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;
}
}