Search code examples
javaarraysqueuereverse

Reversing queue with array java


Hello I was trying to figure out how to reverse a queue with the use of an array. Ive attached the queue class and a the runner class which created the queue and adds the elements inside of it. The reverse method I have creates an array and my thought would be to check the elements remove and add it to the array created. Im new to queue and an a little lost. Thanks for any help an advanced.

    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()){
         
        }
    }
  
  }

Solution

  • I think you have the right idea so far. Assuming all of your queue methods work, such as size, enqueue, dequeue, etc., then all you have do is as you dequeue elements off of the queue one by one, insert those elements from the end of the array towards the start. You can have a counter that keeps track of where you want to insert the element in the array. This counter would start at Q.size() - 1 since queues follow the FIFO, first-in-first-out principle. Then, with the help of a helper method, you can set the items variable in your queue to revQue after you've populated the elements of the array with the items of the original queue in reverse order. For example, you could modify your reverseQueue method to look something like this,

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

    And this is what the setItems method would look like, added to your 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;
    }
    

    Just a note that the setItems method here assumes perfect circumstances, meaning that the items parameter passed in has a valid element at each spot within the array. For the reverseQueue method, this should work assuming that your other queue methods work as intended. But keep in mind that the setItems method can cause problems if you pass in an array with gaps in it; for example, null elements at certain indices.