使用toString打印循环队列

时间:2017-10-09 20:20:54

标签: java data-structures queue circular-buffer circular-list

我试图找到一种更简单的方法来打印圆形阵列队列。这是我到目前为止所拥有的。

    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方法,我已经使用了两个for循环来打印队列。

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但是当前面==后面它不起作用。 我希望只使用一个for循环来使这个方法顺序(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;    
}

使用此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

如果有人能提出任何令人敬畏的建议。对不起,如果我没有解释清楚的话。

2 个答案:

答案 0 :(得分:0)

我会尝试这样的事情:

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

答案 1 :(得分:0)

int num = front;

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

这似乎对我有用。