![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9ac60775bdc4ed771460ff35fedbcdc8.png)
汉诺塔:
c++
class CQueue {
public:
stack<int> stk1;
stack<int> stk2;
CQueue() {
}
void appendTail(int value) {
while(!stk1.empty()){
stk2.push(stk1.top());
stk1.pop();
}
stk1.push(value);
while(!stk2.empty()){
stk1.push(stk2.top());
stk2.pop();
}
}
int deleteHead() {
if (stk1.empty()){
return -1;
}
int temp = stk1.top();
stk1.pop();
return temp;
}
};
python
class CQueue:
def __init__(self):
self.stk1 = []
self.stk2 = []
def appendTail(self, value: int) -> None:
while self.stk1:
self.stk2.append(self.stk1[-1])
self.stk1.pop()
self.stk1.append(value)
while self.stk2:
self.stk1.append(self.stk2[-1])
self.stk2.pop()
def deleteHead(self) -> int:
if not self.stk1:
return -1
temp = self.stk1[-1]
self.stk1.pop()
return temp
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fdf309a9b497e4e491582a4cbaf3a53a.png)