Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
algorithm to copy the content of stack s1 to s2 using a while loop
Posted
Updated 9-Apr-12 20:36pm
v2

1 solution

Assuming that you only have push, pop, peek and IsEmpty operations you need two loops, and an intermediate stack:
In the first loop, you pop each item off the stack, and push it onto the temporary stack.
In the second loop, you pop each item off the temporary stack and push it onto both the original stack and the output stack.

If you can traverse the start from the bottom, then it isn't a stack copy operation, it's just a memory copy job.
 
Share this answer
 
Comments
Chandrasekharan P 10-Apr-12 2:59am    
Appreciate your effort but Was he expecting a code? :)
OriginalGriff 10-Apr-12 3:23am    
Since it sounds like homework, he won't get it! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900