-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10866_1.py
77 lines (75 loc) · 2.01 KB
/
10866_1.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
class Node():
def __init__(self, value=None, prev=None, next=None):
self.value = value
self.prev = prev
self.next = next
class Deque():
def __init__(self):
self.first = Node()
self.last = Node()
self.first.next = self.last
self.last.prev = self.first
self.length = 0
def push_front(self,x):
temp = Node(x, self.first, self.first.next)
self.first.next.prev = temp
self.first.next = temp
self.length +=1
def push_back(self, x):
temp = Node(x, self.last.prev, self.last)
self.last.prev.next = temp
self.last.prev = temp
self.length +=1
def pop_front(self):
if self.first.next is self.last:
print(-1)
else:
print(self.first.next.value)
self.first.next = self.first.next.next
self.first.next.prev = self.first
self.length -=1
def pop_back(self):
if self.first.next is self.last:
print(-1)
else:
print(self.last.prev.value)
self.last.prev = self.last.prev.prev
self.last.prev.next = self.last
self.length -= 1
def size(self):
print(self.length)
def empty(self):
if self.length ==0:
print(1)
else:
print(0)
def front(self):
if self.length ==0:
print(-1)
else:
print(self.first.next.value)
def back(self):
if self.length ==0:
print(-1)
else:
print(self.last.prev.value)
n = int(input())
d = Deque()
for _ in range(n):
l = input()
if "push_front" in l:
d.push_front(int(l[11:]))
elif "push_back" in l:
d.push_back(int(l[10:]))
elif l == "pop_front":
d.pop_front()
elif l=="pop_back":
d.pop_back()
elif l=="size":
d.size()
elif l=="empty":
d.empty()
elif l =="front":
d.front()
elif l =="back":
d.back()