Dequeue in a Priority queue using a Sorted Dictionary in C# -
i have sorted dictionary of form:
sorteddictionary<prioritytype, list<t>> dictionary;
where prioritytype enum class.
now trying make priority queue methods have apprehensions whether dequeue method work or not.
public t dequeue() { if (isempty()) { throw new exception("the priority queue empty! dequeuing not possible!"); } var highestprioritylist = dictionary[dictionary.keys.first()]; var topelement = highestprioritylist.firstordefault(); if (highestprioritylist.count == 0) { dictionary.remove(dictionary.keys.first()); } highestprioritylist.removeat(0); return topelement; }
please me improve method!
note: dequeue() method supposed remove , return object highest priority , before other elements same priority.
okay able modify above code suit dequeue operation!
public t dequeue() { var highestprioritylist = dictionary[dictionary.keys.first()]; if (highestprioritylist.count == 0) { dictionary.remove(dictionary.keys.first()); } var topelement = highestprioritylist.first(); highestprioritylist.remove(topelement); return topelement; }
now can dequeue long without invalidoperationexception caused missing element in list after removeat operation!
Comments
Post a Comment