2010-12-05 3 views
1
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; 
} 
+0

あなたの質問がありますか? –

+2

数日前に同じ質問がありました。宿題? – sje397

+0

質問はあなたがこのページに行くためにクリックしたものです。 – Tony

答えて

0

さてあなたは、少なくともequeueで次の手順を実行する必要があります。

newNode.setLink(front); 

実は、私はfrontはいつもrear.getLink() througアクセスすることができますので、あなたがfrontrearの両方が必要とは思いません。ここで

は提案です:

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; 
    } 
} 
+0

enqueue()では、まずFrontをNewNodeに保存してからRearを変更する必要があります。今、front()は設定されていないnewNodeからリンクを取得します。 – royas

+0

ああ、良いキャッチ。更新しました。 – aioobe

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; 
    } 
} 

私はこれは古い記事です知っているが、それだけで使用しているので、私は最近、この問題でのトラブルがあったし、これが尋ねた質問に沿って、より多くのことを信じていますリアノードを使用します。

関連する問題