0
How to reverse the output stack without changing the function declarations
https://code.sololearn.com/cE5PZ2WYo0V0/?ref=app The copy constructor should have the same stack as the constructor. No changes on the class or function declaration
1 Respuesta
+ 2
Sample implementation:
Stack::Stack(Stack const& other) {
if (other.top_) {
this->top_ = new Node(other.top_->letter);
for (auto it = other.top_->down, cur = this->top_; it; it = it->down, cur = cur->down) {
cur->down = new Node(it->letter);
}
}
}
If there is at least one element, we establish the top element, and afterwards copy the rest of the stack by repeatedly stepping through the `down` pointer.