Skip to content

Instantly share code, notes, and snippets.

@bmanojkumar
Created August 6, 2014 04:29
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/5916f5bcd71e4bc02e04 to your computer and use it in GitHub Desktop.
Save bmanojkumar/5916f5bcd71e4bc02e04 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);
}
}
void collapse() {
if(s.isEmpty())
return;
if(s.size() % 2 == 1) {
int x = s.pop();
collapse();
s.push(x);
}
else {
int x = s.pop() + s.pop();
collapse();
s.push(x);
}
}
void equls() {
if(s.isEmpty() && cps.isEmpty()) {
System.out.println("Stacks are equal");
return;
}
if(s.peek() == cps.peek()) {
int x = s.pop();
int y = cps.pop();
equls();
s.push(x);
cps.push(y);
}
else {
System.out.println("Not equal");
return;
}
}
}
class qs {
Queue<Integer> q;
Stack<Integer> s;
qs() {
q = new LinkedList<Integer>();
s = new Stack<Integer>();
}
void insert(int x) {
q.add(x);
}
void rearrange() {
if(q.isEmpty()) {
while(!s.isEmpty())
q.add(s.pop());
return;
}
else {
if (q.peek() % 2 == 1) {
int x = q.poll();
rearrange();
q.add(x);
} else {
s.push(q.poll());
rearrange();
}
}
}
}
public class stack_split {
public static void main(String[] args) {
qs s = new qs();
s.insert(1);
s.insert(2);
s.insert(3);
s.insert(4);
s.insert(5);
s.insert(6);
System.out.println(s.q);
s.rearrange();
System.out.println(s.q);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment