Question :
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
Solution:
void reverse()
{
....int a = s.pop();
....if(s.count!=1)
........reverse();
....Push(a);
}
void Push(int a)
{
....int m = s.pop();
....if(s.count!=0)
........Push(a);
....Else
........s.push(a);
....s.push(m);
}
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
Solution:
void reverse()
{
....int a = s.pop();
....if(s.count!=1)
........reverse();
....Push(a);
}
void Push(int a)
{
....int m = s.pop();
....if(s.count!=0)
........Push(a);
....Else
........s.push(a);
....s.push(m);
}
Powered by ScribeFire.
No comments:
Post a Comment