【问题标题】:Why my Queue class work like stack? [closed]为什么我的 Queue 类像堆栈一样工作? [关闭]
【发布时间】:2014-11-30 22:13:39
【问题描述】:

我需要有关 java 编程的帮助。谁能告诉我哪里做错了???

我的 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("]");
   }

   }

【问题讨论】:

  • 旁注:在equals()中,代码boolean flag = false; for ..., if (!flag) return false;可以替换为if (!(otherQ instanceof QueueADT)) return false;
  • 旁注 2:编写测试,使用 TDD ;)
  • 如果这应该是你提到的循环队列,为什么它会自动调整大小?

标签: java arrays stack queue


【解决方案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?

【讨论】:

    【解决方案2】:

    您的队列存在严重问题。

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

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

    【讨论】:

      猜你喜欢
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 2012-01-01
      • 2011-07-10
      • 2011-10-06
      • 2020-12-23
      • 2021-05-14
      相关资源
      最近更新 更多