This is just for fun so you will see tons of print statements for trouble shooting. I was hoping on slimming this down some and gain new ideas on what I have. This was a challenge question in a class I'm not in. I know the code i wrote is nightmare-ish anything to slim it down would be nice.
import java.util.*;
public class StackQueue {
Stack stack1 = new Stack();
Stack stack2 = new Stack();
Stack temp = new Stack();
public void enqueue(String element) {
stack1.push(element);
System.out.println(stack1.toString() + "Stack1");
}
public void dequeue(){
System.out.println(stack1.toString() + "\n\nStack1");
if(!stack1.empty() & stack1.size()> 1 & !stack2.empty()) { // if there are elements in both stacks
System.out.println("IM IN THE BLOCK");
while (!stack2.empty()) {
temp.push(stack2.pop());
System.out.println(temp.toString() + " TEMPP----");
System.out.println(stack2.toString() + " STACK 2---");
}
while (!temp.empty()) {
stack1.push(temp.pop());
System.out.println(stack1.toString() + " STACK1 ----");
}
System.out.println(stack1.pop() + " Has been removed");
System.out.println(stack1.toString() + "STACK 1----");
System.out.println(stack2.toString()+ "STACK 2----");
if(stack2.empty()){
while (!stack1.empty()) {
temp.push(stack1.pop());
System.out.println(temp.toString() + " temp ----");
}
System.out.println(stack1.addAll(temp));
System.out.println(stack1.toString()+ "STACK ! ADD");
temp.removeAllElements();
System.out.println(temp.toString() + "TEMP _++++_+_+_+_");
}
}
else if(stack1.empty()){
System.out.println("\n\nstack1 is empty ");
System.out.println(stack2.pop() + " Has been removed");
}else {
System.out.println("\n\n");
while (!stack1.empty()) {
System.out.println(stack2.toString() + "Stack2 - second part");
stack2.push(stack1.pop());
}
System.out.println(stack1.toString()+ "stack1");
System.out.println(stack2.toString()+"stack2");
System.out.println(stack2.pop() + " Has been removed");
}
}
}
public class Main {
public static void main(String[] args){
StackQueue queue = new StackQueue();
queue.enqueue("hey");
queue.enqueue("I");
queue.enqueue("am");
queue.enqueue("Jeremiah");
queue.enqueue("now");
queue.dequeue(); //removes hey
queue.dequeue(); //removes i
queue.dequeue();// removes am
queue.enqueue("one ");
queue.enqueue("more");
queue.enqueue("time");
queue.dequeue(); // removes jeremiah
// i had to reverse the stack1 order here
queue.dequeue(); // removes now
//queue.dequeue(); // removes one
queue.enqueue("FIRST");
queue.dequeue();//removes FIRST
System.out.println(queue.stack1.toString() + " END S1");
System.out.println( queue.stack2.toString() + " END S2");
System.out.println(queue.temp.toString() + " END temp");
}
}