Created
March 4, 2015 05:09
-
-
Save rohith2506/153abfc76987a7bf8852 to your computer and use it in GitHub Desktop.
Reverse a stack without using external memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// but it uses much more memory than normal external memory | |
void reverse(stk){ | |
if(!stk.empty()){ | |
int temp = stk.top(); | |
stk.pop(); | |
reverse(stk); | |
insert(temp, stk); | |
} | |
} | |
void insert(item, stk){ | |
if(stk.empty()){ | |
stk.push(item); | |
} | |
else { | |
int temp = stk.top(); | |
stk.pop(); | |
insert(temp, stk); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment