2017-10-09 3 views
0

순환 배열 대기열을 인쇄하는 더 쉬운 방법을 찾으려고합니다. 여기까지 내가 지금까지 가지고있는 것이있다.toString을 사용하여 순환 대기열

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


} 

여기 내 주요이다

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"); 


} 

출력 :

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

그래서 기본적으로 당신은 내가이 사용했던 내 toString 메소드를 보면 큐를 인쇄를 들어-반복합니다.

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"; 
      }  
     } 

     return temp;  
    } 

이 toString을 구현하려했지만 앞면 == 뒤쪽 일 때는 작동하지 않습니다. 이 메서드 order (n)를 만들기 위해 for 루프 만 사용하기를 바랬습니다. 이 toString을 사용하여

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

출력 : 사람이 무엇을 제안 할 수

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

하면 좋지 않을까. 미안하지만, 내가 충분히 명확하게 설명하지 않았다면.

답변

0
int num = front; 

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

.

0

이 같은 뭔가를 시도 할 것입니다 : 이것은 나를 위해 작동하는 것 같다

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

나는 아주 비슷한 것을 할 수있었습니다. 답장을 보내 주셔서 감사합니다! –

+0

좋습니다. 그럼 답으로 표시해 주시겠습니까? 감사. –

관련 문제