/****
Given a stack S, write a C program to sort the stack (in the ascending
order).
We are not allowed to make any assumptions about how the stack is implemented.
The only functions to be used are:
Push
Pop
Top
IsEmpty
IsFull
****/
void recursive(Stack s)
{
if(s.isEmpty == true)
return ;
elem temp = s.pop();
recursive(s);
recur_push(temp, s);
return;
}
recur_push(elem t, stack s)
{
if(s.isEmpty == null || s.top() > t)
{
s.push(t);
return;
}
temp1= s.pop()
recur_push(t,s);
s.push(temp1);
}
2 comments:
YOu are anyway gonna use O(n) memory space on the runtime stach. You might as well want to allocate that much space and then quick sort and then do a Push n times.
Stack In,Out,Temp;
Out.Push(In.pop());
while(!In.isEmpty()){
int top_val = In.top();
if (top_val > Out.top())
Out.Push(top_val);
else{
while(top_val < Out.top())
Temp.Push(Out.pop());
Out.Push(top_val);
while(!Temp.IsEmpty())
Out.Push(Temp.pop());
}
}
Post a Comment