-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomQueue.py
117 lines (85 loc) · 2.64 KB
/
customQueue.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
from customStack import CustomStack
class InterfaceQueue:
def is_empty(self) -> bool:
pass
def enqueue(self, value):
pass
def dequeue(self):
pass
def print_enqueue(self, value):
pass
def print_dequeue(self):
pass
def print_detailed(self):
pass
class DefaultQueue(InterfaceQueue):
payload = []
head = 0
end = 0
length = 0
def __init__(self, length):
self.length = length
def is_empty(self):
return self.head == self.end
def enqueue(self, value):
if self.is_empty():
self.head = 1
self.end = 1
self.payload[self.end - 1:] = value,
if self.end == self.length:
self.end = 1
else:
self.end += 1
def dequeue(self):
value = self.payload[self.head - 1]
if self.head == self.length:
self.head = 1
else:
self.head += 1
return value
def print_enqueue(self, value):
self.enqueue(value)
print("enqueue", value)
print(self)
def print_dequeue(self):
print("dequeue", self.dequeue())
print(self)
def print_detailed(self):
print(self, 'head:', self.head, 'end:', self.end, 'length:', self.length)
def __str__(self):
p = self.payload
end = self.end - 1
str_payload = [p[i] if i < len(p) else '' for i in range(self.length)]
str_payload = [x if not x == p[self.head - 1] else f'[{x}]' for x in str_payload]
str_payload[end] = f'|{str_payload[end]}|'
return f"{str_payload}"
class StackQueue(InterfaceQueue):
length = 1
head = CustomStack(length)
end = CustomStack(length)
def __init__(self, length):
self.length = length
self.head = CustomStack(length)
self.end = CustomStack(length)
def is_empty(self):
return self.end.is_empty() and self.head.is_empty()
def enqueue(self, value):
self.end.push(value)
def dequeue(self):
if self.is_empty():
raise ValueError('Underflow')
if self.head.is_empty():
while not self.end.is_empty():
self.head.push(self.end.pop())
return self.head.pop()
def print_enqueue(self, value):
self.enqueue(value)
print("enqueue", value)
print(self)
def print_dequeue(self):
print("dequeue", self.dequeue())
print(self)
def print_detailed(self):
print(self, 'end_top:', self.end.top, 'head_top:', self.head.top, 'length:', self.length)
def __str__(self):
return f"end: {self.end}, head:{self.head}"