-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplement-queue-by-two-stack.py
executable file
·58 lines (48 loc) · 1.21 KB
/
implement-queue-by-two-stack.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 28 14:44:41 2020
@author: johnoyegbite
"""
# SOLVED!
class MyQueue:
def __init__(self):
# do intialization if necessary
self.stack = []
self.queue = []
self.front = -1
"""
@param: element: An integer
@return: nothing
"""
def push(self, element):
# write your code here
if self.front < 0:
self.front = 0
self.stack.append(element)
return (self.stack, self.front)
"""
@return: An integer
"""
def pop(self):
# write your code here
if -1 < self.front < len(self.stack):
elem_del = self.stack[self.front]
self.front += 1
return elem_del
"""
@return: An integer
"""
def top(self):
# write your code here
if -1 < self.front < len(self.stack):
# print("top():", self.front)
return self.stack[self.front]
if __name__ == "__main__":
q = MyQueue()
print(q.push(1))
print(q.pop())
print(q.push(2))
print(q.push(3))
print(q.top())
print(q.pop())
# print(q.top())