public void enqueue(Object element)
// Adds element to the rear of this queue.
{
LLObjectNode newNode = new LLObjectNode(element);
if (rear == null)
front = newNode;
else
rear.setLink(newNode);
rear = newNode;
}
public Object dequeue()
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes front element from this queue and returns it.
{
if (isEmpty())
throw new QueueUnderflowException("Dequeue attempted on empty queue.");
else
{
Object element;
element = front.getInfo();
front = front.getLink();
if (front == null)
rear = null;
return element;
}
}
public boolean isEmpty()
// Returns true if this queue is empty; otherwise, returns false.
{
if (front == null)
return true;
else
return false;
}
5516 次
2 に答える
2
public class CircLinkedUnbndQueue<T> implements UnboundedQueueInterface<T>
{
protected LLNode<T> rear; // reference to the rear of this queue
public CircLinkedUnbndQueue()
{
rear = null;
}
public void enqueue(T element)
// Adds element to the rear of this queue.
{
LLNode<T> newNode = new LLNode<T>(element);
if (rear == null)
{
rear = newNode;
}
else
{
//links the newNode to the rear node's pointer and then 're'points the
//rear node to the newNode.
if(rear.getLink() == null)
{
rear.setLink(newNode);
newNode.setLink(rear);
}
else
{
newNode.setLink(rear.getLink());
rear.setLink(newNode);
}
}
//'repositions' the reat node at the end of the queue.
rear = newNode;
}
public T dequeue()
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes front element from this queue and returns it.
{
if (isEmpty())
throw new QueueUnderflowException("Dequeue attempted on empty queue.");
else
{
T element;
rear = rear.getLink();
element = rear.getInfo();
if (rear.getLink() == null)
rear = null;
return element;
}
}
public boolean isEmpty()
// Returns true if this queue is empty; otherwise, returns false.
{
if (rear == null)
return true;
else
return false;
}
}
これが古い投稿であることは知っていますが、最近この問題で問題が発生しました。これは、リア ノードのみを使用するため、尋ねられた質問により近いと思います。
于 2012-11-27T01:42:23.573 に答える
0
さて、あなたは少なくとも次のことをする必要がありますequeue
:
newNode.setLink(front);
実際、私はあなたが両方を必要とするとは思わないので、front
いつrear
でもfront
アクセス可能rear.getLink()
です。
ここに提案があります:
public class CircularLinkedList {
LLObjectNode rear;
// Adds element to the rear of this queue.
public void enqueue(Object element) {
LLObjectNode newNode = new LLObjectNode(element);
if (!isEmpty())
rear.setLink(newNode);
LLObjectNode front = front();
rear = newNode;
// Set new nodes successor to front
newNode.setLink(front);
}
private LLObjectNode front() {
return rear.getLink();
}
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes front element from this queue and returns it.
public Object dequeue() {
if (isEmpty())
throw new QueueUnderflowException(
"Dequeue attempted on empty queue.");
Object element = front().getInfo();
// Exclude front from list
if (onlyOneLeft())
rear = null;
else
rear.setLink(front().getLink());
return element;
}
private boolean onlyOneLeft() {
return front() == rear;
}
public boolean isEmpty() {
// Returns true if this queue is empty; otherwise, returns false.
return rear == null;
}
}
于 2010-12-05T07:49:43.280 に答える