forked from nryoung/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.py
More file actions
54 lines (41 loc) · 1.46 KB
/
queue.py
File metadata and controls
54 lines (41 loc) · 1.46 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Queue
-----
A Queue is a linear data structure, or more abstractly a sequential
collection. The entities in the collection are kept in order and the
principal (or only) operations on the collection are the addition of
entities to the rear terminal position, known as enqueue, and removal of
entities from the front terminal position, known as dequeue. This makes the
queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure,
the first element added to the queue will be the first one to be removed.
Pseudo Code: https://en.wikipedia.org/wiki/Queue_%28abstract_data_type%29
"""
from collections import deque
class Queue:
queue_list = deque([])
def __init__(self):
self.queue_list = deque([])
def add(self, value):
"""
Add element as the last item in the Queue.
Worst Case Complexity: O(1)
"""
self.queue_list.append(value)
def remove(self):
"""
Remove element from the front of the Queue and return it's value.
Worst Case Complexity: O(1)
"""
return self.queue_list.popleft()
def is_empty(self):
"""
Returns a boolean indicating if the Queue is empty.
Worst Case Complexity: O(1)
"""
return not len(self.queue_list)
def size(self):
"""
Return size of the Queue.
Worst Case Complexity: O(1)
"""
return len(self.queue_list)