为什么我的Queue类像堆栈一样工作?

时间:2014-11-30 22:13:40

标签: java arrays stack queue

我需要java编程方面的帮助。谁能告诉我我做错了什么????

My Queue类的工作方式类似于Stack,而不是Queue。如果我将1,2和3排队(按此顺序)然后出列,则删除3而不是1。

提前谢谢!

这是一个圆形数组,这里是我的代码:

import java.util.NoSuchElementException;


public class Queue implements QueueADT
{
   private int front = 0;
   private int back = 0;
   private int [] a;
   private int size = 10;


   public Queue(){
       a = new int [10];
   }

   public Queue(int size){
       a = new int [size];
       this.size = size;
   }


   //enqueue - adds an element to the back of the queue
   public void enqueue(int element){

       if(((back+1) - front) == -1 || ((back+1) - front) == (a.length - 1))
           resizeArray();

       if (back == a.length - 1)
           back = 0;

           a[back++] = element;


   }

   //dequeue - removes and returns the element from the
   //front of the queue
   public int dequeue(){
       if(isEmpty())
           throw new NoSuchElementException("Dequeue: Queue is empty");


       if (front < back){
       front ++;
       return a[front-1];
       }

       else if (front > back){
       front--;
       return a[front++];  
       }

       return 1;
       }


   //peek - returns but does not remove the element from
   //the front of the queue
   public int peek(){
       if(isEmpty())
   throw new NoSuchElementException("Peek: Queue is empty");
   return a[front];
   }

   //isEmpty - determines if the queue is empty or not
   public boolean isEmpty(){
       return size() == 0;

   }

   private void resizeArray()
   {
       //double the size of the array
       int[] newA = new int[a.length * 2];
       int x = 0;
       while(x < size - 1){
           newA[x] = dequeue();

           x++;
       }
       size = size *2;
       front = 0;
       back = x;
       a = newA;

       }



   //size - returns the number of elements in our Queue
   public int size(){

       if (front > back ){
       return size - (front - (back + 1));}

       return back - front + 1;}



   //toString - returns a String representation of our Queue
   public String toString(){

       if(isEmpty()) {
throw new NoSuchElementException("Queue is empty");
       }

       {
       String s = "[ ";

       //print queue
       for(int i = 0; i < size(); i++)
           s += a[i] + " ";

       //print array
       for(int j = size(); j < a.length; j++)
           s += "* ";

       s += "]";
       return s;
       }
   }


   //equals - determines if two queues are equivalent
   //i.e. do they have the same elements in the same sequence
   //ignoring the structure they are stored in
   public boolean equals(Object otherQ){

           if(otherQ == null)
               return false;
           else
           {
               //Figure out how many interfaces the other guy
               //implements
               int numIs = otherQ.getClass().getInterfaces().length;
               boolean flag = false;

               //look at all of the other guys interfaces
               //and if he doesn't implement QueueADT, then
               //he clearly isn't a Queue and we return false
               for(int i = 0; i < numIs; i++)
               {
                   if(this.getClass().getInterfaces()[0] ==
                       otherQ.getClass().getInterfaces()[i])
                   {
                       flag = true;
                   }
               }
               if(!flag)
                   return false;
               else //we know that the other guy exists and
                   //we know that he implements QueueADT
               {
                   QueueADT q = (QueueADT)otherQ;
                   if(this.size() != q.size())
                       return false;
                   else
                   {
                       boolean queuesEqual = true;
                       for(int i = 0; i < this.size(); i++)
                       {
                           int ourElement = this.dequeue();
                           int qElement = q.dequeue();

                           if(ourElement != qElement)
                               queuesEqual = false;

                           this.enqueue(ourElement);
                           q.enqueue(qElement);
                       }
                       //return our final answer
                       return queuesEqual;
                   }
               }


           }
       }


   public void showInnerWorkings(){
       System.out.print("[");
       for (int i = 0; i < this.a.length; i++)
           System.out.print(this.a[i] +
                           (i != this.a.length - 1 ? ", " : ""));
       System.out.println("]");
   }

   }

2 个答案:

答案 0 :(得分:1)

您的队列行为不像堆栈。它的行为就像一个有缺陷的队列。

  public static void main(String[] args) {
    Queue q = new Queue();
    q.enqueue(7);
    q.enqueue(8);
    q.enqueue(9);
    System.out.println(q.dequeue()); // 7 (ok)
    System.out.println(q.dequeue()); // 8 (ok)
    System.out.println(q.dequeue()); // 9 (ok)
    System.out.println(q.dequeue()); // 1 (bug)
    System.out.println(q.dequeue()); // 1 (bug)
    System.out.println(q.dequeue()); // 1 (bug)
  }

你的代码在哪里让你得出结论,它是在第一次调用dequeue时删除了3?

答案 1 :(得分:1)

你的队列严重错误。

这是一个单元测试的开始,它测试你的队列并显示一些错误。

import org.junit.Test;

import java.util.NoSuchElementException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;


public class QueueTest {

final Queue queue = new Queue();

@Test
public void givenNewQueue_thenSizeIsZero() {
    assertEquals(0, queue.size());
}

@Test
public void givenNewQueue_thenIsEmpty() {
    assertTrue(queue.isEmpty());
}

@Test(expected = NoSuchElementException.class)
public void givenNewQueue_whenDequeing_thenThrowsException() {
    queue.dequeue();
}

@Test
public void givenNewQueue_whenQueueingElement_thenIsNotEmpty() {
    queue.enqueue(0xA113);
    assertFalse(queue.isEmpty());
}

@Test
public void givenNewQueue_whenQueueingElement_thenSizeIsOne() {
    queue.enqueue(0xA113);
    assertEquals(1, queue.size());
}

@Test
public void givenNewQueue_whenQueueingElement_thenReturnsElement() {
    queue.enqueue(0xA113);
    assertEquals(0xA113, queue.dequeue());
}
}