2017-10-09 7 views
0

J'essaie de trouver un moyen plus simple d'imprimer la file d'attente circulaire. Voici ce que j'ai jusqu'ici.Utilisation de toString pour imprimer la file d'attente circulaire

public class CircularArrayQueue<T> implements QueueADT<T> 
    { 
     private final static int DEFAULT_CAPACITY = 100; 
     protected int front, rear, count; 
     private T[] queue; 

    /** 
    * Creates an empty queue using the specified capacity. 
    * @param initialCapacity the initial size of the circular array queue 
    */ 
    public CircularArrayQueue (int initialCapacity) 
    { 
     front = rear = 0; 
     count = 0; 
     queue = (T[]) (new Object[initialCapacity]); 
    } 

    /** 
    * Creates an empty queue using the default capacity. 
    */ 
    public CircularArrayQueue() 
    { 
     this(DEFAULT_CAPACITY); 
    }  

    /** 
    * Adds the specified element to the rear of this queue, expanding 
    * the capacity of the queue array if necessary. 
    * @param element the element to add to the rear of the queue 
    */ 
    public void enqueue(T element) 
    { 
     if (size() == queue.length) 
      expandCapacity(); 

     //assign element to the queue 
     queue[rear] = element; 

     //update the rear of queue 
     rear = (rear+1) % queue.length; 

     count++; 
    } 

    /** 
    * Removes the element at the front of this queue and returns a 
    * reference to it. 
    * @return the element removed from the front of the queue 
    * @throws EmptyCollectionException if the queue is empty 
    */ 
    public T dequeue() throws EmptyCollectionException 
    { 
     if (isEmpty()) 
      throw new EmptyCollectionException("queue"); 

     //remove from front of the queue 
     T result = queue[front]; 
     queue[front] = null; 

     //update the front of the queue 
     front = (front+1) % queue.length; 

     //update the queue size 
     count--; 

     return result; 
    } 

    /** 
    * Creates a new array to store the contents of this queue with 
    * twice the capacity of the old one. 
    */ 
    private void expandCapacity() 
    { 
     T[] larger = (T[]) (new Object[queue.length *2]); 

     for (int scan = 0; scan < count; scan++) 
     { 
      larger[scan] = queue[front]; 
      front = (front + 1) % queue.length; 
     } 

     front = 0; 
     rear = count; 
     queue = larger; 
    } 



    /** 
    * Returns true if this queue is empty and false otherwise. 
    * @return true if this queue is empty 
    */ 
    public boolean isEmpty() 
    { 
     return count == 0; 
    } 

    /** 
    * Returns the number of elements currently in this queue. 
    * @return the size of the queue 
    */ 
    public int size() 
    { 
     return count; 
    } 

    /** 
    * Returns a string representation of this queue. 
    * @return the string representation of the queue 
    */ 
    public String toString() 
    { 
     String temp = ""; 

     if(isEmpty()) 
      System.out.println("\nCircular Queue is Empty!!!\n"); 
     else{ 
      for(int i = front; i < queue.length; i++){ 
       if(queue[i] != null) 
        temp += "queue[" + i + "] = " + queue[i] + "\n"; 

      } 

      for(int i = 0; i < rear; i++){ 
       if(queue[i] != null) 
        temp += "queue[" + i + "] = " + queue[i] + "\n"; 
      } 

     //This seems like it would work if the front != rear 
     /* for(int i = front; i != rear; i = (i + 1) % queue.length){ 

       temp += "queue[" + i + "] = " + queue[i] + "\n"; 
       System.out.println("queue[" + i + "] = " + queue[i] + "\n"); 
      }*/ 

     } 

     return temp;  
    } 


} 

Voici ma principale:

public static void main(String[] args) throws EmptyCollectionException 

{ 
    CircularArrayQueue<Integer> arrayQueue = new CircularArrayQueue<>(5); 
    //Add to the queue 
    arrayQueue.enqueue(1); 
    arrayQueue.enqueue(2); 
    arrayQueue.enqueue(3); 
    arrayQueue.enqueue(4); 
    arrayQueue.enqueue(5); 

    //Print info about the queue 
    System.out.println("\n***");   
    System.out.println("count = " + (arrayQueue.count)); 
    System.out.println("front = " + (arrayQueue.front)); 
    System.out.println("rear = " + arrayQueue.rear); 
    System.out.print(arrayQueue.toString()); 
    System.out.println("***\n"); 

    //Removes two elements from the front of the queue 
    arrayQueue.dequeue(); 
    arrayQueue.dequeue(); 

    //Displays the info after removing two elements from the queue 
    System.out.println("AFTER DQ"); 
    System.out.println("\n***"); 
    //Print info about the queue 
    System.out.println("count = " + (arrayQueue.count)); 
    System.out.println("front = " + (arrayQueue.front)); 
    System.out.println("rear = " + arrayQueue.rear); 
    System.out.print(arrayQueue.toString()); 
    System.out.println("***\n"); 


    //Adds two elements to the back of the queue. 
    arrayQueue.enqueue(6); 
    arrayQueue.enqueue(7); 

    //Displays the info after adding two elements to the back of queue 
    System.out.println("AFTER ENQUEUE"); 
    System.out.println("\n***");   
    System.out.println("count = " + (arrayQueue.count)); 
    System.out.println("front = " + (arrayQueue.front)); 
    System.out.println("rear = " + arrayQueue.rear); 
    System.out.print(arrayQueue.toString()); 
    System.out.println("***\n"); 


} 

sortie:

***Zero index of the queue is the front to start*** 

//After placing 5 elements in the queue the front and rear are the same. 
//So if we were to add another element to the rear it would override the 
//index 0 and then rear would get updated to index 1(if we didn't expand the array). 
count = 5 
front = 0 
rear = 0 
queue[0]=1 
queue[1]=2 
queue[2]=3 
queue[3]=4 
queue[4]=5 

//After removing 2 elements from the front of the queue. The front value changes. 
//The rear is still the same. 
count = 3 
front = 2 
rear = 0 
queue[2]=3 
queue[3]=4 
queue[4]=5 

//After adding 2 more elements to the queue. The front and the rear are the same again and the queue is full. 
count = 5 
front = 2 
rear = 2 
queue[2]=3 
queue[3]=4 
queue[4]=5 
queue[0]=6 
queue[1]=7 

Donc, fondamentalement, si vous regardez ma méthode toString je l'ai utilisé deux pour boucles d'imprimer la file d'attente. J'ai essayé d'implémenter cette toString mais cela ne fonctionne pas quand l'avant == arrière. J'espérais n'utiliser qu'une seule boucle pour rendre cette méthode ordonnée (n).

public String toString(){ 
      String temp = ""; 
    //This seems like it would work if the front != rear 
    for(int i = front; i != rear; i = (i + 1) % queue.length){ 

      temp += "queue[" + i + "] = " + queue[i] + "\n";    
    } 
    return temp;  
} 

sortie en utilisant ce toString:

***Zero index of the queue is the front to start*** 

//After placing 5 elements in the queue the front and rear are the same. 
//So if we were to add another element to the rear it would override the 
//index 0 and then rear would get updated to index 1(if we didn't expand the array). 
count = 5 
front = 0 
rear = 0 


//After removing 2 elements from the front of the queue. The front value changes. 
//The rear is still the same. 
count = 3 
front = 2 
rear = 0 
queue[2]=3 
queue[3]=4 
queue[4]=5 

//After adding 2 more elements to the queue. The front and the rear are the same again and the queue is full. 
count = 5 
front = 2 
rear = 2 

Si quelqu'un pourrait suggérer tout ce qui serait génial. Désolé si je n'ai rien expliqué de suffisamment clair.

Répondre

0
int num = front; 

     for(int i = 0; i < count; i++){ 
      temp += "CircularQueue[" + num + "] = " + queue[num] +"\n"; 
      num = (num + 1) % queue.length; 
     } 

Cela semble fonctionner pour moi.

0

Je voudrais essayer quelque chose comme ceci:

int i = 0; 
while(i < size) 
{ 
    int index = (i + front) % size 
    if(queue[index] != null); 
     System.out.println("queue[" + index + "] = " + queue[index]); 
    i++; 
} 
+0

J'ai été capable de faire quelque chose de très similaire. Merci pour votre réponse! –

+0

c'est bien. Alors pouvez-vous s'il vous plaît marquer comme la réponse? Merci. –