-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.py
37 lines (28 loc) · 889 Bytes
/
queue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from collections import deque
class Queue:
def __init__(self,first_item=None):
self.items = deque()
if first_item:
self.items.append(first_item)
def enqueue(self, item):
self.items.append(item)
def enqueue_items(self, items):
self.items.extend(items)
def dequeue(self):
if not self.is_empty():
return self.items.popleft()
else:
raise IndexError("dequeue from empty queue")
def is_empty(self):
return len(self.items) == 0
def peek(self):
if not self.is_empty():
return self.items[0]
else:
raise IndexError("peek from empty queue")
def __reversed__(self):
return reversed(self.items)
def __iter__(self):
return iter(self.items)
def __len__(self):
return len(self.items)