Hi there I'm trying to implement the stack methods push(x) and pop() with two queue's. I was able to test out my methods using print statements in the methods themselves so I know the methods are working properly, but I know that's bad coding practice. I've looked all over about issues regarding the strange output (@15db9742) when I try to print the stack during the for loop in main. All of the solutions I've seen are directed at classes that use strings, so they just say to override the toString() method to print out what you need, but my push(x) method doesn't return anything, and if I choose to return int it obviously doesn't work. The other solutions I've seen are trying Arrays.toString(), but these aren't arrays either. The last solution I've looked into is implementing Iterator, but I can't seem to extrapolate the way the examples implementation is used to my own.
public class Assignment1Question1B {
Queue q1 = new LinkedList();
Queue q2 = new LinkedList();
int front;
int n;
public class Node {
int x;
Node next;
}
public void push(int x) {
q1.add(x);
front = x;
n++;
}
public void pop() {
if (q1.size() == 0){
System.out.println("Stack is already empty.");
} else {
while (q1.size() != 1) {
q2.add(q1.remove());
}
q1.remove();
Queue empty = q1;
q1 = q2;
q2 = empty;
n--;
}
}
public static void main(String[] args) {
Assignment1Question1B stack = new Assignment1Question1B();
for (int i = 0; i < 50; i++) {
stack.push(i);
System.out.println(stack);
}
}
}