/** * Initialize your data structure here. */ publicMyQueue() { }
/** * Push element x to the back of queue. */ publicvoidpush(int x) {// 本质就是拿备胎来进行数据转移 if (stack.isEmpty()) { stack.push(x); return; } while (!stack.isEmpty()) { temp.push(stack.pop()); } temp.push(x); while (!temp.isEmpty()) { stack.push(temp.pop()); } }
/** * Removes the element from in front of queue and returns that element. */ publicintpop() { return stack.pop(); }
/** * Get the front element. */ publicintpeek() { return stack.peek(); }
/** * Returns whether the queue is empty. */ publicbooleanempty() { return stack.isEmpty(); } }