Yahoo Interview Question
Software Engineer / Developers#include <stack>
#include <iostream>
using namespace std;
typedef stack<int> Stack;
void push_to_bottom(Stack& stack, int x);
void reverse(Stack& stack)
{
if (!stack.empty()) {
int x = stack.top();
stack.pop();
reverse(stack);
push_to_bottom(stack, x);
}
}
void push_to_bottom(Stack& stack, int x)
{
if (stack.empty()) {
stack.push(x);
}
else {
int y = stack.top();
stack.pop();
push_to_bottom(stack, x);
stack.push(y);
}
}
// destructive
void dump_stack(Stack& s)
{
while (!s.empty()) {
int i = s.top();
s.pop();
cout << i << ' ';
}
cout << endl;
}
int main()
{
Stack s;
// 5 at bottom
s.push(5);
s.push(4);
s.push(3);
s.push(2);
s.push(1);
reverse(s);
dump_stack(s);
}
jd@shade:/mnt/raid/projects/interviewQs$ ./swap-stack-recursively
5 4 3 2 1
Prateek you had it apart from small errors.
If we were allowed to use 'size()', we could have done a loop at the top, popping off the top, and pushing it to the bottom, size() times. But this way it's pure recursion.
void returnSorted(stack &s)
{
if(s.IsEmpty)
return;
else
{
int val = stack.pop();
returnSorted(s);
appendToSorted(val, s);
}
}
void appendToSorted(int val, stack s)
{
if (s.IsEmpty())
s.push(val);
else
{
int temp = s.top();
if (val > temp)
{
appendToSorted(val, s.pop());
s.push(temp);
}
else
{
if(!s.isFull())
s.push(val);
}
}
}
I think the following should work... could someone comment?
void stackRec(Node* lastPopped) {
if (!isEmpty()) {
stackRec(pop());
}
push(lastPopped);
return;
}
int main() {
stackRec(NULL);
return 0;
}
it's really easy one
import java.util.*;
class StackRev
{
public static void main(String args[])
{
Stack<Integer> s=new Stack<Integer>();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no. of no.s to be pushed ");
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
System.out.println("Enter");
s.push(sc.nextInt());
}
reverse(s);
System.out.println("Stack after reversal is ");
while(!s.isEmpty())
System.out.println(s.pop());
}
public static void reverse(Stack<Integer> s)
{
if(s.isEmpty())
return;
int x=s.pop();
reverse(s);
s.push(x);
}
}
void ReverseStack(stack<int>& stk)
{
if(stk.empty())
return;
int elem = stk.top();
stk.pop();
ReverseStack(stk);
Reverse(stk, elem);
}
void Reverse(stack<int>& stk, int elem)
{
if(stk.empty())
stk.push(elem);
else
{
int data = stk.top();
stk.pop();
Reverse(stk, elem);
stk.push(data);
}
}
We can use another temp stack to store the elements pop from the original stack, this shouldn't be regarded as using extra memory ( except constant pointer overhead )
public void ReverseStack(Stack* s1)
{
// create an empty stack
Stack* s2 = new Stack();
ReverseStackRecur(s1,s2);
}
public void ReverseStackReCur(s1, s2)
{
if((s1 == null)||(s1.IsEmpty))
{
return;
}
s2.Push(s1.Pop());
ReverseStackReCur(s1,s2);
s1.Push(s2.Pop());
}
reverse(struct node* stack)
- PrateekCaire September 13, 2010{
int x;
while(!IsEmpty(stack))
{
x = pop(stack);
reverse(stack);
swap(stack, x);
}
}
void swap(struct node* stack, int x)
{
if(!IsEmpty(stack))
{
int y = pop(stack);
swap(stack, x);
push(stack,y);
}
else
{
push(stack,y);
}
}
}