Removing the tail element of the priority queue

How to remove a priority queue tail element? I am trying to implement a ray search using a priority queue, and as soon as the priority queue is full, I want to delete the last element (the element with the lowest priority).

Thank!

+6
source share
6 answers

There is no easy way. Copy the elements from the original to the new, except for the last.

PriorityQueue removelast(PriorityQueue pq)
{

    PriorityQueue pqnew;

    while(pq.size() > 1)
    {
        pqnew.add(pq.poll());
    }

    pq.clear();
    return pqnew;
}

called

pq = removelast(pq);
+5
source

You could use Guava MinMaxPriorityQueue to do this. It provides methods for viewing, polling, and deleting for both ends of the queue.

- , , . offer, add addAll . - :

public class BoundedQueue<E> implements Serializable, Iterable<E>, Collection<E>, Queue<E> {
    private final Queue<E> queue;
    private int capacity;

    public BoundedQueue(Queue<E> queue, int capacity) {
        this.queue = queue;
        this.capacity = capacity;
    }

    @Override
    public boolean offer(E o) {
        if (queue.size() >= capacity)
            return false;
        return queue.add(o);
    }

    @Override
    public boolean add(E o) throws IllegalStateException {
        if (queue.size() >= capacity)
            throw new IllegalStateException("Queue full"); // same behavior as java.util.ArrayBlockingQueue
        return queue.add(o);
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        boolean changed = false;
        for (E o: c)
            changed |= add(o);
        return changed;
    }

    // All other methods simply delegate to 'queue'
}
+5

. , , .

+2

, PR- - , , PQ, , .

PQ , , (queue[0]), , .

, - PQ :

    public class MyPriorityQueue<E> extends PriorityQueue<E>
    {
        // constructors

    public E getTail()
     {
        // queue.length can be bigger than this.size() !!
        Object[] queue = this.toArray();
        E tail = (E)queue[0];
        Comparator<? super E> comparator = this.comparator();
        if (comparator !=null)
           for(int i = 1; i < this.size(); i++)
              if ( comparator.compare(tail, (E)queue[i]) < 0)
                 tail = (E)queue[i];
        else
           for(int j = 1; j < this.size(); j++)
              if ( ((Comparable)tail).compareTo( ((Comparable)queue[j]) ) < 0 )
                 tail = (E)queue[j];
        return tail;
     }  

     public E removeTail()
     {
        E tail = this.getTail();
        this.remove(tail);
        return tail;
     }   
    }
+2

, .

, , , , PriorityQueue.remove(Object )

Iterator<E> it = Queue.iterator();
while (it.hasNext()) {
   temp<E> = it.next();
   counter++;
   if (counter == Queue.size()) {
       Queue.remove(temp);
    }
}
0

, . .

1) PriorityQueue → CustomQueue.java 2) removeLast() 3) , ( )

public void removeLast() {
    if(size == 0) {
       return;
    }
    queue[size - 1] = null;
    size--;

}

The reason for this is because the PriorityQueue implementation uses an array to store the object. Thus, "size" introduces a pointer to the next available spot in the array. Reducing it, the size of the array / queue decreases, as if you were discarding the last element.

-3
source

All Articles