Skip to content

Instantly share code, notes, and snippets.

@bmanojkumar
Created August 5, 2014 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmanojkumar/f9a860ca799af03ef2a7 to your computer and use it in GitHub Desktop.
Save bmanojkumar/f9a860ca799af03ef2a7 to your computer and use it in GitHub Desktop.
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
* Created by manoj on 6/8/14.
*/
class split {
Stack<Integer> s;
Stack<Integer> cps;
Queue<Integer> q = new LinkedList<Integer>();
split() {
s = new Stack<Integer>();
cps = new Stack<Integer>();
}
void insert(int x) {
s.push(x);
}
void sort() {
if(s.isEmpty()) {
while(!q.isEmpty())
s.push(q.remove());
return;
}
else {
if (s.peek() > 0) {
int t = s.pop();
sort();
s.push(t);
} else {
q.add(s.pop());
sort();
}
}
}
void stutter() {
if(s.isEmpty())
return;
else {
int x = s.pop();
stutter();
s.push(x);
s.push(x);
}
}
void copystack() {
if(s.isEmpty())
return;
else {
int x = s.pop();
copystack();
s.push(x);
cps.push(x);
}
}
}
public class stack_split {
public static void main(String[] args) {
split s = new split();
s.insert(1);
s.insert(-12);
s.insert(-4);
s.insert(8);
s.insert(45);
s.insert(-9);
s.copystack();
System.out.println(s.s);
System.out.println(s.cps);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment