用数组 java 反转队列

Reversing queue with array java

你好,我想弄清楚如何使用数组来反转队列。我附加了队列 class 和一个创建队列并在其中添加元素的运行器 class。我已经创建了一个数组的反向方法,我的想法是检查删除的元素并将其添加到创建的数组中。我刚开始排队,有点迷路。感谢任何高级帮助。

    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(
                "Trying to enqueue into a full queue");
    }       

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

     // queue created with reverse method

      public class RunnerQueue {
     public static void main(String args[]){
      Queue q = new Queue();
      q.enqueue(10);
      q.enqueue(20);
      q.enqueue(30);
      q.enqueue(40);
      q.enqueue(50);

    public static void reverseQueue(Queue Q){
         int[] revQue = new int(Q.size);
        While(!Q.isEmpty()){
         
        }
    }
  
  }

到目前为止,我认为您的想法是正确的。假设所有队列方法都有效,例如 sizeenqueuedequeue 等,那么您所要做的就是将元素一个一个地从队列中取出,插入从数组末尾到开头的那些元素。您可以有一个计数器来跟踪要在数组中插入元素的位置。该计数器将从 Q.size() - 1 开始,因为队列遵循 FIFO、先进先出原则。然后,在辅助方法的帮助下,您可以将队列中的 items 变量设置为 revQue 在您以相反的顺序使用原始队列的项目填充数组的元素之后。例如,您可以将 reverseQueue 方法修改为如下所示,

public static void reverseQueue(Queue Q){
    int[] revQue = new int[Q.size()];
    int i = Q.size() - 1;
    while(!Q.isEmpty()){
        revQue[i] = Q.dequeue();
        i--;
    }
    Q.setItems(revQue);
}

这就是 setItems 方法的样子,添加到您的 Queue class,

public void setItems(Object[] items) {
    this.items = items;
    this.QUEUE_SIZE = items.length;
    this.front = 0;
    this.back = items.length - 1;
    this.count = items.length;
}

请注意,这里的 setItems 方法假设完美的情况,这意味着传入的 items 参数在数组中的每个位置都有一个有效元素。对于 reverseQueue 方法,假设您的其他队列方法按预期工作,这应该可以工作。但是请记住,如果您传入一个有间隙的数组,setItems 方法可能会导致问题;例如,某些索引处的空元素。

这是更新class:

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(
                    "Trying to enqueue into a full queue");
    }

    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 an 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 static void reverseQueue(Queue q)
    {
        if(q.isEmpty())
        {
            return;
        }

        Object data = q.peek();
        q.dequeue();
        reverseQueue(q);
        q.enqueue(data);
    }

    public static void printQueue(Queue q)
    {
        while(!q.isEmpty())
        {
            System.out.println(q.dequeue());
        }
    }

    public static void main(String[] args)
    {
        Queue q = new Queue();
        q.enqueue(10);
        q.enqueue(20);
        q.enqueue(30);
        q.enqueue(40);
        q.enqueue(50);

        reverseQueue(q);
        printQueue(q);
    }
}

所以想法是从队列中弹出元素,如果它有元素的话。对队列中剩余的值调用 reverseQueue 方法,然后将从队列中移除的值放回队列中。以相反的顺序制作。