class PriorityNode:
def __init__(self, data=None, priority=None, next=None):
self.data = data
self.priority = priority
self.next = next
class PriorityQueue:
def __init__(self):
self.front = None
self.rear = None
def clear(self):
self.front = None
self.rear = None
def isEmpty(self):
return self.front is None
def offer(self, x, priority):
s = PriorityNode(x, priority, None)
if not self.isEmpty():
p = self.front
q = self.front
while p is not None and p.priority <= s.priority:
q = p
p = p.next
if p is None:
self.rear.next = s
self.rear = s
elif p == self.front:
s.next = self.front
self.front = s
else:
q.next = s
s.next = p
else:
self.front = self.rear = s
def poll(self):
if self.isEmpty():
return None
p = self.front
self.front = self.front.next
if p == self.rear:
self.rear = None
return p.data
l = PriorityQueue()
l.offer(1, 20)
l.offer(2, 30)
l.offer(3, 10)
print(l.poll())
print(l.poll())
print(l.poll())
3
1
2