Home » Stacks & Queues(Use Me !!) » Reverse Stack Inplace

Reverse Stack Inplace


import java.util.Stack;

public class ReverseStackInPlace {
Stack<Integer> s;
public void ReverseInPlaceStack() {
s = new Stack<Integer>();
}
/**
 * call this function to reverse inplace stack
 */

 public void reverseStack() {
if (!s.isEmpty()) {
int data = s.pop();
reverseStack();
pushInStack(data);
}
}
public void pushInStack(int a) {
if (s.isEmpty()) {

System.out.print(" "+s.push(a));
}
int b = s.pop();
pushInStack(a);
s.push(b);
}

 public static void main(String[] args) {

ReverseStackInPlace st = new ReverseStackInPlace();
}

}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Website

arpit tak

arpit tak

I like JAVA . I code. I chill. I blog.I eat. I sleep. I dream.

Personal Links

View Full Profile →

Followers

%d bloggers like this: