-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueuecustom.py
More file actions
57 lines (52 loc) · 1.61 KB
/
Copy pathqueuecustom.py
File metadata and controls
57 lines (52 loc) · 1.61 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
class Queue:
def __init__(self, capacity):
"""
Initializes the Queue with a specified capacity.
:param capacity: The maximum size of the queue.
"""
self.array = [None] * capacity
self.max_size = capacity
self.front = 0
self.back = -1
self.current_size = 0
def is_empty(self):
"""
Checks if the queue is empty.
:return: True if the queue is empty, False otherwise.
"""
return self.current_size == 0
def get_current_size(self):
"""
Gets the current size of the queue.
:return: The number of elements in the queue.
"""
return self.current_size
def is_full(self):
"""
Checks if the queue is full.
:return: True if the queue is full, False otherwise.
"""
return self.current_size == self.max_size
def enqueue(self, value):
"""
Adds an element to the back of the queue.
:param value: The value to be added.
"""
if self.is_full():
print("Queue Full")
return
self.back = (self.back + 1) % self.max_size
self.array[self.back] = value
self.current_size += 1
def dequeue(self):
"""
Removes and returns the front element of the queue.
:return: The element at the front of the queue.
"""
if self.is_empty():
print("Queue is Empty")
return None
value = self.array[self.front]
self.front = (self.front + 1) % self.max_size
self.current_size -= 1
return value