-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircleQueue.py
38 lines (29 loc) · 1019 Bytes
/
CircleQueue.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
class CircleQueue():
def __init__(self, k):
self.k = k
self.queue = [None] * k
self.head = self.tail = -1
# Добавляем элемент в круговую очередь
def add_element(self, data):
if ((self.tail + 1) % self.k == self.head):
return False
elif (self.head == -1):
self.head = 0
self.tail = 0
self.queue[self.tail] = data
else:
self.tail = (self.tail + 1) % self.k
self.queue[self.tail] = data
# Удаляем элемент из очереди
def del_element(self):
if (self.head == -1):
return False
elif (self.head == self.tail):
temp = self.queue[self.head]
self.head = -1
self.tail = -1
return temp
else:
temp = self.queue[self.head]
self.head = (self.head + 1) % self.k
return temp