46 lines
889 B
Java
46 lines
889 B
Java
package com.xgiovio.esercizi;
|
|
|
|
import exceptions.EmptyStackException;
|
|
import priorityqueue.PriorityQueue;
|
|
import priorityqueue.heap.HeapPriorityQueue;
|
|
import stack.Stack;
|
|
|
|
/**
|
|
* Created with MONSTER.
|
|
* User: xgiovio
|
|
* Date: 03/06/2014
|
|
* Time: 13:50
|
|
*/
|
|
public class PQStack<E> implements Stack<E> {
|
|
|
|
private int priority = 0;
|
|
private PriorityQueue<Integer,E> p = new HeapPriorityQueue<Integer, E>();
|
|
|
|
|
|
@Override
|
|
public void push(E element) {
|
|
priority--;
|
|
p.insert(priority,element);
|
|
}
|
|
|
|
@Override
|
|
public E top() throws EmptyStackException {
|
|
return p.min().getValue();
|
|
}
|
|
|
|
@Override
|
|
public E pop() throws EmptyStackException {
|
|
return p.removeMin().getValue();
|
|
}
|
|
|
|
@Override
|
|
public boolean isEmpty() {
|
|
return (size()== 0);
|
|
}
|
|
|
|
@Override
|
|
public int size() {
|
|
return p.size();
|
|
}
|
|
}
|