representing Stack using Queues

can anyone help me with the concept of simulating a stack using 2 queues? I'm not quite sure the theory behind this or how to begin implementing it. thanks for the help!
[177 byte] By [johnsmith954a] at [2007-11-26 19:22:52]
# 1
You only need one queue. You provide methods to add (to the end of the queue) and to remove (from the end of the queue). What has led you to believe you need two queues?
floundera at 2007-7-9 21:43:55 > top of Java-index,Java Essentials,New To Java...
# 2
the homework called for simulating basic stack operations using two queues.
johnsmith954a at 2007-7-9 21:43:56 > top of Java-index,Java Essentials,New To Java...
# 3
If the assignment text is not too long, post it here. If it is too long just post the relevant section(s).
floundera at 2007-7-9 21:43:56 > top of Java-index,Java Essentials,New To Java...
# 4

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();

}

}

johnsmith954a at 2007-7-9 21:43:56 > top of Java-index,Java Essentials,New To Java...
# 5

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.

floundera at 2007-7-9 21:43:56 > top of Java-index,Java Essentials,New To Java...