2017-04-17 22 views
0

我一直在嘗試將隊列的大小加倍,並且我嘗試了幾種不同的方法,但這個方法給了我最接近的結果,它將隊列中已有的所有值複製到新創建的空間中,而不是將它們留空以供我添加更多的對象。 我有兩個類,我需要改變的唯一部分是enqueue方法。如何在java中將隊列大小加倍

enter image description here

最後一行應該打印50 40 30 20 60 70代替。

public class Queue{ 
    private int QUEUE_SIZE = 5; 
    private Object[] items; 
    private int front, back, count; 
    public Queue() { 
     items = new Object[QUEUE_SIZE]; 
     front = 0; 
     back = QUEUE_SIZE -1; 
     count =0; 
    } 
    public boolean isEmpty(){ 
     return count ==0; 
    } 
    public boolean isFull(){ 
     return count == QUEUE_SIZE; 
    } 
    public void enqueue(Object newItem){ 
     if (!isFull()){ 
      back = (back+1) % QUEUE_SIZE; 
      items[back] = newItem; 
      count++; 
      return; 
     } else 
      System.out.println("Queue is full. Doubling the size."); 
      count = (QUEUE_SIZE*2); 
      System.out.println("New max size is: " + QUEUE_SIZE); 
    } 
    public Object dequeue(){ 
     if (!isEmpty()){ 
      Object queueFront = items[front]; 
      front = (front+1) % QUEUE_SIZE; 
      count--; 
      return queueFront; 
     }else 
      System.out.println("Trying to dequeue from empty queue"); 
     return null; 
    } 
    public void dequeueAll(){ 
     items = new Object[QUEUE_SIZE]; 
     front = 0; 
     back = QUEUE_SIZE -1; 
     count =0; 
    } 
    public Object peek(){ 
     if (!isEmpty()) { 
      return items[front]; 
     } 
     else 
      System.out.println("Trying to peek with empty queue"); 
     return null; 
    } 

    public int size(){ 
     return count; 
    } 

} 






import java.util.Stack; 

public class Runner { 

    public static void main(String[] args) { 
    Queue q = new Queue(); 
    createQueue(q); 
    System.out.println("My queue is as follows: "); 
    printQueue(q); 
    System.out.println("I am going to dequque one element."); 
    q.dequeue(); 
    System.out.println("My queue is as follows: "); 
    printQueue(q); 
    System.out.println("I am going to reverse my queue: "); 
    reverseQueue(q); 
    System.out.println("My queue is as follows: "); 
    printQueue(q); 
    System.out.println("I am going to enqueue 60."); 
    q.enqueue(60); 
    System.out.println("My queue is as follows: "); 
    printQueue(q); 
    System.out.println("I am going to enqueue 70."); 
    q.enqueue(70); 
    System.out.println("My queue is as follows: "); 
    printQueue(q); 
    } 

    public static Queue createQueue(Queue q){ 
     q.enqueue(10); 
     q.enqueue(20); 
     q.enqueue(30); 
     q.enqueue(40); 
     q.enqueue(50); 
     return q; 
    } 

    public static void printQueue(Queue q){ 
     int s = q.size(); 

     for(int i=0; i<s; i++){ 
      int temp = (int)q.dequeue(); 
      q.enqueue(temp); 
      System.out.print(temp+ " "); 
     } 
     System.out.println(); 
    } 

    public static void reverseQueue(Queue q){ 
     Stack s = new Stack(); 
     while(!q.isEmpty()){ 
      s.push(q.dequeue()); 
     }while(!s.isEmpty()){ 
      q.enqueue(s.pop()); 
     } 
    } 

} 

回答

0

enqueue(Object newItem)else塊不會插入新對象,所以它應該是這樣的:

public void enqueue(Object newItem) { 
    if (!isFull()) { 
     back = (back + 1) % QUEUE_SIZE; 
     items[back] = newItem; 
     count++; 
     return; 
    } 
    else { 
     System.out.println("Queue is full. Doubling the size."); 
     QUEUE_SIZE = (QUEUE_SIZE * 2); // double queue size not count 
     System.out.println("New max size is: " + QUEUE_SIZE); 
     Object[] temp = new Object[QUEUE_SIZE]; // temp array 
     System.arraycopy(items, front, temp, front, items.length - front); // copy the elements from front index to items.length-front 
     if (front != 0) { 
      System.arraycopy(items, 0, temp, items.length, front); // copy the elements in the range items[0] to items[back] into the new array 
     } 
     items = temp; // set items to temp array 
     back = front + count; 
     items[back] = newItem; // set new item 
     count++; // increment count 
    } 

} 
+0

非常感謝你,這真的幫助我理解我在做什麼錯。 –

+0

@DanielaGarcia歡迎您! – fiskra

0

它是關於printQueue()方法,在count =(QUEUE_SIZE * 2)之後; enqueue()方法中,items數組的大小實際上並未展開。無論如何,它仍然是5,count =(QUEUE_SIZE * 2);提供isFull()方法返回false。

如果您調試printQueue()方法,您會看到int s = 10,但項目數組仍然是[30,20,60,50,40]。

0

隊列變滿後,您不排隊項目。因此該項目在變滿後未插入到隊列中。請修改代碼。

package org.sunil.addressbook.dao; 
import java.util.Stack; 

class Queue{ 
    private int QUEUE_SIZE = 5; 
    private Object[] items; 
    private int front, back, count; 
    public Queue() { 
     items = new Object[QUEUE_SIZE]; 
     front = 0; 
     back = QUEUE_SIZE -1; 
     count =0; 
    } 
    public boolean isEmpty(){ 
     return count ==0; 
    } 
    public boolean isFull(){ 
     return count == QUEUE_SIZE; 
    } 
    public void enqueue(Object newItem){ 
     if (!isFull()){ 
      back = (back+1) % QUEUE_SIZE; 
     // System.out.println("back output is "+back); 
      items[back] = newItem; 
      count++; 
     } else{ 
      System.out.println("Queue is full. Doubling the size."); 
      back=QUEUE_SIZE-1; 
      QUEUE_SIZE = (QUEUE_SIZE*2); 
      Object[] temp = new Object[QUEUE_SIZE]; 
      int j=0; 
       for (int i = front; i < items.length; i++){ 
        temp[j] = items[i]; 
        j++; 
       } 
       for (int i = 0; i < front; i++){ 
        temp[j] = items[i]; 
        j++; 
       } 
       items = temp; 
       front=0; 
       back = (back+1) % QUEUE_SIZE; 
      items[back] = newItem; 
      count++; 
     } 
    } 
    public Object dequeue(){ 
     if (!isEmpty()){ 
      Object queueFront = items[front]; 
      front = (front+1) % QUEUE_SIZE; 
      count--; 
      return queueFront; 
     }else 
      System.out.println("Trying to dequeue from empty queue"); 
     return null; 
    } 
    public void dequeueAll(){ 
     items = new Object[QUEUE_SIZE]; 
     front = 0; 
     back = QUEUE_SIZE -1; 
     count =0; 
    } 
    public Object peek(){ 
     if (!isEmpty()) { 
      return items[front]; 
     } 
     else 
      System.out.println("Trying to peek with empty queue"); 
     return null; 
    } 

    public int size(){ 
     return count; 
    } 

} 
public class Runner { 

    public static void main(String[] args) { 
    Queue q = new Queue(); 
    createQueue(q); 
    System.out.println("My queue is as follows: "); 
    printQueue(q); 
    System.out.println("I am going to dequque one element."); 
    q.dequeue(); 
    System.out.println("My queue is as follows: "); 
    printQueue(q); 
    System.out.println("I am going to reverse my queue: "); 
    reverseQueue(q); 
    System.out.println("My queue is as follows: "); 
    printQueue(q); 
    System.out.println("I am going to enqueue 60."); 
    q.enqueue(60); 
    System.out.println("My queue is as follows: "); 
    printQueue(q); 
    System.out.println("I am going to enqueue 70."); 
    q.enqueue(70); 
    System.out.println("My queue is as follows: "); 
    printQueue(q); 
    } 

    public static Queue createQueue(Queue q){ 
     q.enqueue(10); 
     q.enqueue(20); 
     q.enqueue(30); 
     q.enqueue(40); 
     q.enqueue(50); 
     return q; 
    } 

    public static void printQueue(Queue q){ 
     int s = q.size(); 

     for(int i=0; i<s; i++){ 
      int temp = (Integer)q.dequeue(); 
      q.enqueue(temp); 
      System.out.print(temp+ " "); 
     } 
     System.out.println(); 
    } 

    public static void reverseQueue(Queue q){ 
     Stack s = new Stack(); 
     while(!q.isEmpty()){ 
      s.push(q.dequeue()); 
     }while(!s.isEmpty()){ 
      q.enqueue(s.pop()); 
     } 
    } 

}