C Puzzles

yet another place for C puzzles

Thursday, October 20, 2005

 

reverse a stack in place !!!

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

Comments:
/* stack reversal in place. */
stack_rev(stack * s)
{
if(IsEmpty(s) )
return;
top = pop(s);
stack_rev(s)
just_fly_and_back(s, top)
}

/* unwind the stack till end, put the backup , rewind it back */
just_fly_and_back(stack *s, int prev_top)
{
if (isEmpty(s)) {
push(s, prev_top)
return;
}
b = pop(s);
just_fly_and_back(s, prev_top);
push(s, b)
}
 
Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

July 2005   August 2005   October 2005   December 2005   March 2006   June 2006   July 2006   December 2006   February 2007   June 2007   March 2010   May 2010  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]