0
Why is my stack is printed in inverse if i call the copy constructor?
I recently made a copy constructor but when Iâm printing the copy it print them in inverse order for some reason. https://code.sololearn.com/cZDW1RRiQpin/?ref=app
4 Answers
+ 2
Your copy constructor
```
Stack::Stack(Stack const& other){
Node *temp = other.top_;
while(temp != nullptr){
char value = temp->letter;
push(value);
temp = temp->down;
}
}
```
Is simply reading from the first stack ( top to bottom ) and then pushing those values in the new stack ( bottom to top ).
Of course the values in the new stack would be stored in opposite order.
+ 1
Skadoosg there are multiple ways to do this.
The simplest one I could think of is to simply create another stack ( let's call it temp_stack ) and then first push all the elements of *other* in this stack first and then use the same process to push the elements of *temp_stack* to the destination stack.
The order would be flipped twice, and the destination stack would have same order as that of source.
another approach could be to alter your internal data structure ( linked list in this case ) to something which can traverse both ways ( like a doubly linked list ) to solve the problem.
0
Is there a way that you can doing it without accessing outside funtion? Arsenic
0
I tried your simplest way and my result become double as the data