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

27th Apr 2022, 12:08 AM
Skadoosg
4 Respostas
+ 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.
27th Apr 2022, 12:29 AM
Arsenic
Arsenic - avatar
+ 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.
27th Apr 2022, 12:53 AM
Arsenic
Arsenic - avatar
0
Is there a way that you can doing it without accessing outside funtion? Arsenic
27th Apr 2022, 12:31 AM
Skadoosg
0
I tried your simplest way and my result become double as the data
27th Apr 2022, 8:40 PM
Skadoosg