开发者

How do I make this linked queue circular using only the rear external pointer?

开发者 https://www.devze.com 2023-01-28 18:29 出处:网络
public void enqueue(Object element) // Adds element to the rear of this queue. { LLObjectNode newNode = new LLObjectNode(element);
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 eleme开发者_运维技巧nt;
 }
}

public boolean isEmpty()
// Returns true if this queue is empty; otherwise, returns false.
{
 if (front == null)
   return true;
 else
   return false;
}


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

I know this is an old post but I had trouble with this problem recently and believe this to be more in line with the question asked since it uses only uses a rear node.


Well you at least need to do the following in equeue:

newNode.setLink(front);

Actually, I don't believe you need both front and rear since front will always be accessible throug rear.getLink().

Here is a suggestion:

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

精彩评论

暂无评论...
验证码 换一张
取 消