-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJ_list_queue_v2.py
65 lines (52 loc) · 1.48 KB
/
J_list_queue_v2.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
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def __str__(self):
return str(self.value)
class ListQueue:
def __init__(self, head=None, tail=None):
self.head = head
self.tail = tail
self.size = 0
def put(self, item):
item = Node(item)
if self.size == 0:
self.head = self.tail = item
else:
self.tail.next = item
self.tail.next.next = self.head
self.tail = self.tail.next
self.size += 1
def get(self):
if self.size == 0:
return 'error'
result = self.head
if self.size == 1:
self.tail = self.head = None
else:
self.head = self.tail.next.next
self.tail.next = self.head
self.size -= 1
return result
def printed_linked(self):
if self.size == 0:
return None
node = self.head
for i in range(self.size):
print(node.value, end=' -> ')
node = node.next
print('again')
def main():
list_queue = ListQueue()
commands = int(input())
for command in range(commands):
command = input().split()
if command[0] == 'put':
list_queue.put(command[1])
elif command[0] == 'get':
print(list_queue.get())
elif command[0] == 'size':
print(list_queue.size)
if __name__ == '__main__':
main()