#How would I dequeue this in increasing order?

1 messages · Page 1 of 1 (latest)

crimson coral
#
    private static final SortedABPriQ<Integer> priorityQueue = new SortedABPriQ<>();

    public static void randomNumGen() {
        Random rand = new Random();
        for(int i = 0; i < 100; i++){
            int randomNumber = rand.nextInt(10000) + 1;
            priorityQueue.enqueue(randomNumber);
        }
    }

    public static void decreasing(){
        //Print the numbers in decreasing order
        System.out.println("Decreasing order:");//Displaying in 10 columns
        int count = 0;
        while (!priorityQueue.isEmpty()){
            System.out.printf("%-10d", priorityQueue.dequeue());
            count++;
            if (count % 10 == 0) {
                System.out.println();
            }
        }
    }

    public static void increasing(){

}

I got decreasing order down, mainly because the queue already puts it in decreasing, but not increasing. I cannot edit the priority queue code.

sly spireBOT
# crimson coral ```java private static final SortedABPriQ<Integer> priorityQueue = new Sorte...

Detected code, here are some useful tools:

Formatted code
private static final SortedABPriQ<Integer> priorityQueue = new SortedABPriQ<>();
public static void randomNumGen() {
  Random rand = new Random();
  for (int i = 0; i < 100; i++) {
    int randomNumber = rand.nextInt(10000) + 1;
    priorityQueue.enqueue(randomNumber);
  }
}
public static void decreasing() {
  //Print the numbers in decreasing order
  System.out.println("Decreasing order:");
  //Displaying in 10 columns
  int count = 0;
  while (!priorityQueue.isEmpty()) {
    System.out.printf("%-10d", priorityQueue.dequeue());
    count++;
    if (count % 10 == 0) {
      System.out.println();
    }
  }
}
public static void increasing() {
}
#

<@&987246399047479336> please have a look, thanks.

river kestrel
#

can you show us the priority queue code? does it have other methods than enqueue, dequeue and isempty?

crimson coral
# river kestrel can you show us the priority queue code? does it have other methods than enqueue...

hey just saw your msg here you go

  public SortedABPriQ() 
  // Precondition: T implements Comparable
  {
    elements = (T[]) new Object[DEFCAP];
    comp = new Comparator<T>()
    {
       public int compare(T element1, T element2)
       {
         return ((Comparable)element1).compareTo(element2);
       }
    };
  }

  public SortedABPriQ(Comparator<T> comp) 
  {
    elements = (T[]) new Object[DEFCAP];
    this.comp = comp;
  }

  protected void enlarge()
  // Increments the capacity of the priority queue by an amount 
  // equal to the original capacity.
  {
    // Create the larger array.
    T[] larger = (T[]) new Object[elements.length + DEFCAP];
    
    // Copy the contents from the smaller array into the larger array.
    for (int i = 0; i < numElements; i++)
    {
      larger[i] = elements[i];
    }
    
    // Reassign priority queue reference.
    elements = larger;
  }

  public void enqueue(T element) 
  // Adds element to this priority queue.
  {
     if (numElements == elements.length)
       enlarge();
     
     int index = numElements;
     while ((index > 0) && (comp.compare(elements[index - 1], element) > 0))
     {
       elements[index] = elements[index - 1];
       index--;
     }
     elements[index] = element;
     
     numElements++;  
  }

  public T dequeue()
  // Throws PriQUnderflowException if this priority queue is empty;
  // otherwise, removes element with highest priority from this 
  // priority queue and returns it.
  {
    if (!isEmpty())
    {
      T temp = elements[numElements - 1];
      elements[numElements - 1] = null;
      numElements--;
      return temp;
    }
    else
      throw new PriQUnderflowException("dequeue attempted on an empty priority queue.");
  } 


  public boolean isEmpty()
  // Returns true if this priority queue is empty; otherwise, returns false.
  {
    return (numElements == 0);  
  }
 

sly spireBOT
# crimson coral hey just saw your msg here you go ```java public SortedABPriQ() // Precond...

Detected code, here are some useful tools:

Formatted code
public SortedABPriQ() // Precondition: T implements Comparable
  {
  elements = (T[] ) new Object[DEFCAP] ;
  comp = new Comparator<T>() {
    public int compare(T element1, T element2) {
      return ((Comparable) element1).compareTo(element2);
    }
  };
}
public SortedABPriQ(Comparator<T> comp) {
  elements = (T[] ) new Object[DEFCAP] ;
  this .comp = comp;
}
protected void enlarge() // Increments the capacity of the priority queue by an amount 
// equal to the original capacity.
  {
  // Create the larger array.
  T[] larger = (T[] ) new Object[elements.length + DEFCAP] ;
  // Copy the contents from the smaller array into the larger array.
  for (int i = 0; i < numElements; i++) {
    larger[i]  = elements[i] ;
  }
  // Reassign priority queue reference.
  elements = larger;
}
public void enqueue(T element) // Adds element to this priority queue.
  {
  if (numElements == elements.length) enlarge();
  int index = numElements;
  while ((index > 0) && (comp.compare(elements[index - 1] , element) > 0)) {
    elements[index]  = elements[index - 1] ;
    index--;
  }
  elements[index]  = element;
  numElements++;
}
public T dequeue() // Throws PriQUnderflowException if this priority queue is empty;
// otherwise, removes element with highest priority from this 
// priority queue and returns it.
  {
  if (!isEmpty()) {
    T temp = elements[numElements - 1] ;
    elements[numElements - 1]  = null ;
    numElements--;
    return temp;
  }
  else throw new PriQUnderflowException("dequeue attempted on an empty priority queue.");
}
public boolean isEmpty() // Returns true if this priority queue is empty; otherwise, returns false.
  {
  return (numElements == 0);
}
crimson coral
#

I had to delete a bit thanks to character limit, but it was just a toString method and the variables

potent sphinx
sly spireBOT
river kestrel
#

yeah I think you did the best you could do with the situation

#

no other choice than to get them all and reverse them

crimson coral