如何仅使用后外部指针使这个链接队列循环?

发布于 2024-10-05 17:00:03 字数 792 浏览 5 评论 0原文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿挽旧人 2024-10-12 17:00:18

好吧,您至少需要在 equeue 中执行以下操作:

newNode.setLink(front);

实际上,我不认为您需要 frontrear 因为 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;
    }
}

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;
    }
}
归属感 2024-10-12 17:00:15
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;
  }
}

我知道这是一篇旧帖子,但我最近遇到了这个问题,并且相信这更符合所提出的问题,因为它仅使用后节点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文