Use two queues to simulate the basic operations of a stack. The template for the class is given below:
class SimulateStack {
ArrayQueue que1;
ArrayQueue que2;
int size;
// constructor
public SimulateStack() {
que1 = new ArrayQueue();
que2 = new ArrayQueue();
size = 0;
}
/** @return true iff stack is empty */
public boolean empty() {
// your codes here
}
/**
* @return top element of stack
* @throws EmptyStackException
* when the stack is empty
*/
public Object peek() {
// your codes here
}
/** add theElement to the top of the stack */
public void push(Object theElement) {
// your codes here
}
/**
* remove top element of stack and return it
*
* @throws EmptyStackException
* when the stack is empty
*/
public Object pop() {
// your codes here
}
/** Print the content in the stack */
public void printTheStack() {
// your codes here
}
public static void main(String args[]) {
SimulateStack ss = new SimulateStack();
ss.push("1");
ss.push("2");
ss.push("3");
ss.printTheStack();
System.out.println("-");
System.out.println(ss.peek());
System.out.println("-");
ss.pop();
ss.pop();
ss.pop();
ss.pop();
ss.printTheStack();
}
}
I think you will need to go back to the teacher for clarification. I honestly can see no reason why you need two queues. Unless the ArrayQueue class has some funky functionality. So when it comes to manipulating the stack with pop and/or peek you need to transfer Objects from one queue to the other before you can access the correct Object.
P.S. in future when posting code use code tags. Highlight the code and click the code button above message box.