/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sortedlistpriorityqueue;
import java.util.*;

/**
 *
 * @author kvaradar
 */
public class SortedListPriorityQueue<K,V> implements MyPriorityQueue<K,V> {

    protected LinkedList<Entry<K,V>> entries;
    protected Comparator<K> c;

    public SortedListPriorityQueue () {
        entries = new LinkedList<Entry<K,V>>();
        c = new DefaultComparator<K>();
    }

    public SortedListPriorityQueue( Comparator<K> comp) {
        entries = new LinkedList<Entry<K,V>>();
        c = comp;
    }

    public int size() {return entries.size();}

    public boolean isEmpty() {return entries.isEmpty();}

    public Entry<K,V> min() {return entries.get(0);}

    public Entry<K,V> removeMin() {
        Entry<K,V> temp = entries.get(0);
        entries.remove(0);
        return temp;
    }

    public Entry<K,V> insert (K k, V v) {
        boolean smallerElementFound = false;
        Entry<K,V> entry = new MyEntry<K,V>(k,v);
        if (entries.isEmpty()) {
            entries.add(entry);
        }
        else {
            ListIterator<Entry<K,V>> itr = entries.listIterator(0);
            while (itr.hasNext()) {
                if (c.compare(k, itr.next().getKey()) >= 0) {
                    smallerElementFound = true;
                    break;
                }
            }
            if (smallerElementFound) itr.previous();
            itr.add(entry);
        }
        return entry;
    }
}
