-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_queue.py
59 lines (49 loc) · 1.36 KB
/
list_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
class Node:
def __init__(self, value=None, next_elem=None):
self.value = value
self.next = next_elem
def __str__(self):
return self.value
def __repr__(self):
return f"Node(value={self.value}, next_elem={self.next})"
class ListQueue:
def __init__(self):
self.queue_size = 0
self.head = None
def get(self):
if self.head is None:
return 'error'
previous_node = self.head
self.head = self.head.next
self.queue_size -= 1
return previous_node.value
def __iter__(self):
node = self.head
while node is not None:
yield node
node = node.next
def size(self):
return self.queue_size
def put(self, value):
node = Node(value=value)
if self.head is None:
self.head = node
self.queue_size += 1
return
for cur_node in self:
pass
self.queue_size += 1
cur_node.next = node
def main():
cmd_n = int(input())
queue = ListQueue()
for _ in range(cmd_n):
command = input().split()
if command[0] == 'put':
queue.put(int(command[1]))
elif command[0] == 'get':
print(queue.get())
elif command[0] == 'size':
print(queue.size())
if __name__ == '__main__':
main()