-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.py
81 lines (63 loc) · 1.92 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
from typing import Any
class FixedQueue:
class Empty(Exception):
pass
class Full(Exception):
pass
def __init__(self, capacity: int) -> None:
self.no = 0
self.front = 0
self.rear = 0
self.capacity = capacity
self.que = [None] * capacity
def __len__(self) ->int:
return self.no
def is_empty(self) -> bool:
return self.no <= 0
def is_full(self) -> bool:
return self.no >= self.capacity
def enque(self, x: Any) -> None:
if self.is_full():
raise FixedQueue.Full
self.que[self.rear] = x
self.rear += 1
self.no += 1
if self.rear == self.capacity:
self.rear = 0
def deque(self) -> None:
if self.is_empty():
raise FixedQueue.Empty
x = self.que[self.front]
self.front += 1
self.no -= 1
if self.front == self.capacity:
self.front = 0
return x
def peek(self) ->Any:
if self.is_empty():
raise FixedQueue.Empty
return self.que[self.front]
def find(self, value:Any) ->Any:
for i in range(self.no):
idx = (i + self.front) % self.capacity
if self.que[idx] == value:
return idx
return -1
def count(self, value:Any) -> bool:
c = 0
for i in range(self.no):
idx = (i + self.front) % self.capacity
if self.que[idx] == value:
c += 1
return c
def clear(self) -> None:
self.no = self.front = self.rear = 0
def dump(self) ->None:
if self.is_empty():
print('큐ㅜ 빔')
else:
for i in range(self.no):
print(self.que[(i + self.front) % self.capacity], end='')
print()
def __contains__(self, value: Any) -> bool:
return self.count(value)