/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package sortedlistpriorityqueue; import java.util.*; /** * * @author kvaradar */ public class SortedListPriorityQueue implements MyPriorityQueue { protected LinkedList> entries; protected Comparator c; public SortedListPriorityQueue () { entries = new LinkedList>(); c = new DefaultComparator(); } public SortedListPriorityQueue( Comparator comp) { entries = new LinkedList>(); c = comp; } public int size() {return entries.size();} public boolean isEmpty() {return entries.isEmpty();} public Entry min() {return entries.get(0);} public Entry removeMin() { Entry temp = entries.get(0); entries.remove(0); return temp; } public Entry insert (K k, V v) { boolean smallerElementFound = false; Entry entry = new MyEntry(k,v); if (entries.isEmpty()) { entries.add(entry); } else { ListIterator> 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; } }