简体   繁体   English

Java - 我正在将堆栈链表修改为队列链表,但我的dequeue方法仍然像pop一样

[英]Java - I was modifying a stack linked list into a queue linked list but my dequeue method still behaves like pop

private Node back isn't used yet, and enqueue (which was push) and dequeue (which was pop) haven't really been modified except for renaming some things. private Node back还没有被使用,并且enqueue (推送)和dequeue (pop是)除了重命名一些东西之外还没有真正被修改过。 Again, this was originally a stack but I'm trying to modify it into a queue. 同样,这最初是一个堆栈,但我正在尝试将其修改为队列。 I've done non-linked list queues and stacks before with int s, but with objects and linked lists I'm sort of lost. 我在使用int之前已经完成了非链接列表队列和堆栈,但是对于对象和链接列表,我有点迷失。

public class DogQueue 
{
    private Node front = null;
    private Node back = null;
    private Node element = null;
    private int counter = 0;

The above is just setting up variables. 以上只是设置变量。

  private class Node //This sets up the Linked List
                     //Data Structure with nodes.
  {
      private Dog doggy;
      private Node nextNode;
      private Node firstNode;

      Node(Dog newDog)
      {
          doggy = newDog;
      }    
  }

Node stuff which I don't quite understand is above. 我不太了解的节点内容是上面的。

  public void enqueue(Dog aDog) //This should enqueue
                                //an object of type Dog.
  {       
      Node dogNode = new Node(aDog);
      dogNode.nextNode = front;
      counter++;
      front = dogNode;
  }

The above here is unmodified from the push method, just renamed. 上面这里没有修改push方法,只是重命名。

  public Dog dequeue()      //This should output
                            //the first entry in the list.
  {
      Dog firstDog = front.doggy;
      element = front.firstNode;
      counter--;
      return firstDog;
  }

The above here is where I'm having the most trouble- currently it behaves like pop (getting and removing the last entered element in the list). 上面这里是我最麻烦的地方 - 目前它的行为类似于pop(获取和删除列表中最后输入的元素)。

  public boolean isFull()   //Checks to see if List is Full.
  {
      return ( counter == 5 );
  }

I set up the counter to just go up to 5 so I can debug isFull. 我将计数器设置为最多5,所以我可以调试isFull。

  public boolean isEmpty()  //Checks to see if List is empty
  {
      if ( counter == 0 )
        {
            return true;
        } else {
            return false;
        }
  }

This just says if counter is zero, then isEmpty is true (otherwise false). 这只是说如果counter为零,则isEmpty为true(否则为false)。

}

I suck at data structures but I believe your enqueue and dequeue are still behaving like pop and push. 我吮吸数据结构,但我相信你的入队和出队仍然像pop和push一样。 The front should point to the head of the queue and the tail one past the last valid object. 前面应指向队列的头部,尾部指向最后一个有效对象。 So the tail should eventually point to null.. I think it should be something like this: 所以尾巴应该最终指向null ..我认为它应该是这样的:

  public void enqueue(Dog aDog)
     {
         Node dogNode = new Node(aDog);

         counter++;
         if (front == null)
             front = back = dogNode;
         else
         {
             back.nextNode = dogNode;
             back = dogNode;

         }
     }
  public Node dequeue()      
  {
      if(front == null) return null;
      Dog firstDog = front ;
      front = front.nextNode;
      counter--;
      return firstDog;
  }

Here's the main issue. 这是主要问题。 Queues are FIFO (first in, first out), and Stacks are LIFO (last in, first out). 队列是FIFO(先进先出),堆栈是LIFO(后进先出)。 For a queue, the first element you enqueue is the first one you receive, and the most recent element you push onto a stack is the first one you receive. 对于队列,您排队的第一个元素是您收到的第一个元素,并且您推入堆栈的最新元素是您收到的第一个元素。

To that end, let's examine your code a bit. 为此,让我们稍微检查一下你的代码。

  public void enqueue(Dog aDog) { //This should enqueue an object of type Dog.
      Node dogNode = new Node(aDog);
      dogNode.nextNode = front;
      counter++;
      front = dogNode;
  }

You're setting the next node of your new dog element to the front. 您将新狗元素的下一个节点设置在前面。 You would have to go to the end of your queue, set your most recent node to be the new node, and the new node to be null. 您必须转到队列的末尾,将最近的节点设置为新节点,并将新节点设置为null。 Using your code, it would look something like this: 使用您的代码,它看起来像这样:

public void enqueue(Dog aDog) {
    if(front == null) {
        front = new Node(aDog);
        back = front; // back will move later
    } else {
        Node tmp = new Node(aDog);
        tmp.setFirstNode(back);
        back.setNextNode(tmp);
        back = tmp;
    }
}

  public Dog dequeue() {      //This should output the first entry in the list.
      Dog firstDog = front.doggy;
      element = front.firstNode;
      counter--;
      return firstDog;
  }

At least, this does actually show the first thing in the queue. 至少,这确实显示了队列中的第一件事。 But it doesn't actually move the head pointer! 但它实际上并没有移动头指针! Using your code, to do that, it would look something like this: 使用您的代码,为此,它看起来像这样:

public Dog dequeue() {
    if(head == null) {
        return null;
    } else {
        Dog tmp = front.getDoggy()
        front = front.getNextNode(); //move the front to point to the next location
        front.getFirstNode().setNextNode(null); //sever the link to the first element 
        front.setFirstNode(null); //sever the link to the first element
        return tmp;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM