-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.py
136 lines (108 loc) · 3.64 KB
/
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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from collections import deque # noqa: F401
from typing import Optional, List
from exceptions import UnderflowError
class Queue:
"""
# Queue
Is a linear data structure that stores items in First In First Out `(FIFO)` manner;
With a queue the least recently added item is removed first.
"""
def __init__(self, length: int):
"""
Initialize the queue list with fir and number pointers.
:param length: the maximum length size of this queue
:type length: int
"""
self.__length, self.__first, self.__number = length, 0, 0
self.__queue: List[Optional[int]] = [None for _ in range(self.__length)]
def dequeue(self) -> int:
"""
Removes an item from the queue;
The items are popped in the same order in which they are pushed.
Time Complexity: `O(1)`
:raises UnderflowError: if the queue is empty, then it's Underflow exception
:return: the value to be popped
:rtype: int
"""
if self.is_empty:
raise UnderflowError("Queue is empty.")
item, self.__queue[self.__first] = self.__queue[self.__first], None
self.__first = (self.__first + 1) % self.__length
self.__number -= 1
return item
def enqueue(self, value: int):
"""
Adds an item to the queue.
Time Complexity: `O(1)`
:raises OverflowError: if the queue is full, then it's Overflow exception
:param value: the value to be pushed
:type value: int
"""
if self.is_full:
raise OverflowError("Queue is full.")
self.__queue[(self.__first + self.__number) % self.__length] = value
self.__number += 1
@property
def size(self) -> int:
"""
The number size of queue elements.
:return: the queue size number
:rtype: int
"""
return self.__number
@property
def length(self) -> int:
"""
The maximum length of the queue.
:return: the maximum size number
:rtype: int
"""
return self.__length
@property
def front(self) -> int:
"""
First In item added to the queue.
:raises UnderflowError: If the queue is empty
:return: first integer into queue
:rtype: int
"""
if self.__number == 0:
raise UnderflowError("Queue is empty.")
return self.__queue[self.__first]
@property
def rear(self) -> int:
"""
Last In item added to the queue.
:raises UnderflowError: If the queue is empty
:return: last integer into queue
:rtype: int
"""
if self.__number == 0:
raise UnderflowError("Queue is empty.")
return self.__queue[(self.__first + self.__number - 1) % self.__length]
@property
def is_empty(self) -> bool:
"""
Boolean indicating whether the queue is empty.
:return: flag to check if queue is empty
:rtype: bool
"""
return self.__number == 0
@property
def is_full(self) -> bool:
"""
Boolean indicating whether the queue is full.
:return: flag to check if queue is full
:rtype: bool
"""
return self.__number == self.__length
def __repr__(self) -> str: # pragma: no cover
"""
Representation the queue list.
:return: representation string
:rtype: str
"""
items = []
for index in range(self.__number):
items.append(str(self.__queue[(self.__first + index) % self.__length]))
return f"-> _ {' | '.join(reversed(items))} ->"