I'm trying to implement a priority queue using an array list of my PQElement’s. I am stuck on dequeueing from the priority queue the PQ-element with the greatest priority so that I can print elements of the priority queue into a string, in order of decreasing priority. Any idea on how to go about this?
class PQElement: def __init__(self, v, p): self.val = v self.priority = p def __str__(self): return "("+str(self.val)+","+str(self.priority)+")" class PQueue: def __init__(self): self.inList = [] def __len__(self): return len(self.inList) def ___str__(self,): return str(self.inList) def enq(self, e): # insert (tail) self.inList.append(e) def deq(self): # remove if len(self)==0: raise IndexError("deq from empty queue") max_priority = self.inList[0].priority max_idx = 0 for idx, elmt in enumerate(self.inList): if elmt.priority > max_priority: max_priority = elmt.priority max_idx = idx return self.inList.pop(max_idx)
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)