Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created March 4, 2015 05:09
Show Gist options
  • Save rohith2506/153abfc76987a7bf8852 to your computer and use it in GitHub Desktop.
Save rohith2506/153abfc76987a7bf8852 to your computer and use it in GitHub Desktop.
Reverse a stack without using external memory
// 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