-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.py
58 lines (44 loc) · 1008 Bytes
/
9.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
s.
class stack:
def __init__(self):
self._data = []
def push(self,ele):
self._data.append(e)
def pop(self):
return self._data.pop()
def length(self):
return len(self._data)
def display(self):
print(self._data)
class queue:
def __init__(self):
self.s1 = stack()
self.s2 = stack()
def enqueue_rear(self,ele):
if self.s2.length() == 0:
self.s2.push(ele)
else:
self.s1.push(self.s2.pop())
self.s1.push(ele)
for i in range(self.s1.length()):
self.s2.push(self.s1.pop())
#self.s2.display()
def dequeue_front(self):
return self.s2.pop()
def enqueue_front(self,ele):
self.s2.push(ele)
def dequeue_rear(self):
for i in range(self.s2.length()):
self.s1.push(self.s2.pop())
ele = self.s1.pop()
for i in range(self.s1.length()):
self.s2.push(self.s1.pop())
return ele
if __name__ == '__main__':
q = queue()
q.enqueue_rear(10)
q.enqueue_front(20)
q.enqueue_rear(30)
q.enqueue_front(40)
print(q.dequeue_front())
print(q.dequeue_rear())