To be honest, this question is a bit confusing, and it is not clear which functions are available and which are not.
It was later discovered that he was unclear about the characteristics of the data structure.
Note: The
stack is first in and then out. The
queue is last in and out.
Therefore, to use the queue to implement the stack, we can only use
1. pop (0) (pop [0])
2. append (enter to [-1])
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
self.stack = []
self.pre = []
self.pre2 = []
def push(self, x: int) -> None:
"""
Push element x onto stack.
"""
#templist = [x]
#self.stack = templist + self.stack
self.stack.append(x)
def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
self.stack = self.stack[::-1]
res = self.stack.pop(0)
self.stack = self.stack[::-1]
return res
def top(self) -> int:
"""
Get the top element.
"""
print(self.stack)
return self.stack[-1]
def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
print(self.stack)
return True if self.stack == [] else False