Write a C Program to reverse a stack "in place" using recursion ?

**You can only use the following ADT functions on Stack:  
IsEmpty  
IsFull  
Push  
Pop  
Top** 

#include
#include
using namespace std;
stack S;

void func(int n){
if(S.empty())
S.push(n);
else{
int t= S.top();
S.pop();
func(n);
S.push(t);
}
}

void Rec(){
if(!S.empty()){
int t = S.top();
S.pop();
Rec();
func(t);
}
}

void printStack(){
if(!S.empty()){
int t= S.top();
S.pop();
cout«”,";
printStack();
S.push(t);
}
else{
cout«"Stack empty now”;
}

}

int main(int argc, char* argv[])
{

S.push(4);  
S.push(3);  
S.push(2);  
S.push(1);  

printStack();  

Rec();  
printStack();  

return 0;  

}


See also