简体   繁体   中英

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. 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.

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.

  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).

  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.

  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).

}

I suck at data structures but I believe your enqueue and dequeue are still behaving like pop and 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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