Created
August 5, 2014 20:09
-
-
Save bmanojkumar/f9a860ca799af03ef2a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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