我正在尝试为2个相同大小的堆栈编写交换功能。到目前为止,我做到了,但不能正常工作
void swap(stack<int> s1, Stack<int> s2) {
// check for empty stack
if (s1.empty() || s2.empty()) {
return;
}
// check for size
if (s1.size() != s2.size()) {
return;
}
// storing top elements on the stacks
int temp1 = s1.pop();
int temp2 = s2.pop();
// recursive swapping
swap(s1, s2);
// inserting elements
s1.push(temp2);
s2.push(temp1);
}