mirror of
https://github.com/Andreabont/PyExperiments.git
synced 2025-07-14 17:51:15 +00:00
28 lines
606 B
Python
28 lines
606 B
Python
import heapq
|
|
import collections
|
|
|
|
class Queue:
|
|
def __init__(self):
|
|
self.elements = collections.deque()
|
|
|
|
def empty(self):
|
|
return len(self.elements) == 0
|
|
|
|
def put(self, x):
|
|
self.elements.append(x)
|
|
|
|
def get(self):
|
|
return self.elements.popleft()
|
|
|
|
class PriorityQueue:
|
|
def __init__(self):
|
|
self.elements = []
|
|
|
|
def empty(self):
|
|
return len(self.elements) == 0
|
|
|
|
def put(self, item, priority):
|
|
heapq.heappush(self.elements, (priority, item))
|
|
|
|
def get(self):
|
|
return heapq.heappop(self.elements)[1]
|