-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.py
More file actions
62 lines (50 loc) · 1.62 KB
/
queue.py
File metadata and controls
62 lines (50 loc) · 1.62 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
55
56
57
58
59
60
61
62
# -*- coding: UTF_8 -*-
class Queue:
FIFO = 0
LIFO = 1
def __init__(self, l=[], t=FIFO):
"""
An object which implement the queue logic from a list.
:param l: The base list to use with this queue. The list is empty by default.
:param t: The type of the queue FIFO or LIFO. FIFO by default
:type l: list
:type t: int
"""
self.list = l
self.__type = t
def push(self, e):
"""
Push the param at the end of the queue.
:param e: The element to insert in the queue.
"""
self.list.append(e)
def pull(self):
"""
Pull the element as the type of the queue
:raise ValueError: If the queue type is not provided.
:return: The element to pull as the logic of the queue
"""
if self.__type == self.__class__.FIFO:
return self.list.pop(0)
elif self.__type == self.__class__.LIFO:
return self.list.pop(len(self.list) - 1)
else:
raise ValueError("The type must be set to FIFO or LIFO. See the documentation")
return None
def clear(self):
"""
Clear the queue to be empty.
"""
count = len(self.list)
while count > 0:
self.list.pop(0)
count -= 1
def is_empty(self):
"""
Check if the queue is empty or not.
:return: True if the queue is empty False otherwise
:rtype: bool
"""
return len(self.list) == 0
def __str__(self):
return str(self.list)