classMyQueue{ private Stack<Integer>stack1; private Stack<Integer>stack2; /** Initialize your data structure here. */ publicMyQueue(){ stack1 = new Stack<Integer>(); stack2 = new Stack<Integer>(); } /** Push element x to the back of queue. */ publicvoidpush(int x){ stack1.push(x); } /** Removes the element from in front of queue and returns that element. */ publicintpop(){ peek(); return stack2.pop(); } /** Get the front element. */ publicintpeek(){ if(stack2.isEmpty()){ while(!stack1.isEmpty()){ stack2.push(stack1.pop()); } }
return stack2.peek(); } /** Returns whether the queue is empty. */ publicbooleanempty(){ return stack1.isEmpty()&&stack2.isEmpty(); } }
/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */
/** * Your CQueue object will be instantiated and called as such: * CQueue obj = new CQueue(); * obj.appendTail(value); * int param_2 = obj.deleteHead(); */
/** Initialize your data structure here. */ publicMyQueue(){ stack1 = new Stack<Integer>(); stack2 = new Stack<Integer>(); } /** Push element x to the back of queue. */ publicvoidpush(int x){ if(stack1.isEmpty()){ front = x; } while(!stack1.isEmpty()){ stack2.push(stack1.pop()); } stack2.push(x); while(!stack2.isEmpty()){ stack1.push(stack2.pop()); } } /** Removes the element from in front of queue and returns that element. */ publicintpop(){ int s = stack1.pop(); if(!stack1.isEmpty()){ front = stack1.peek(); //j将新的栈顶元素赋值给front } return s; } /** Get the front element. */ publicintpeek(){ return stack1.peek(); } /** Returns whether the queue is empty. */ publicbooleanempty(){ return stack1.isEmpty(); } }
/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */
/** Initialize your data structure here. */ publicMyQueue(){ stack1 = new Stack<Integer>(); stack2 = new Stack<Integer>(); } /** Push element x to the back of queue. */ publicvoidpush(int x){ if(stack1.isEmpty()){ front = x; //代表每次stack1空了之后,再压入的队首元素会更新。 } stack1.push(x); } /** Removes the element from in front of queue and returns that element. */ publicintpop(){ if(stack2.isEmpty()){ while(!stack1.isEmpty()){ stack2.push(stack1.pop()); } } return stack2.pop(); } /** Get the front element. */ publicintpeek(){ if(!stack2.isEmpty()){ return stack2.peek(); } return front; } /** Returns whether the queue is empty. */ publicbooleanempty(){ return stack1.isEmpty()&&stack2.isEmpty(); } } //使用front来保存队首元素,确保在peek()操作时,如果stack2是空的,可以直接返回之前保存的队首元素。 /** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */
/** Initialize your data structure here. */ publicMyStack(){ queue1 = new LinkedList<Integer>(); queue2 = new LinkedList<Integer>();
} /** Push element x onto stack. */ publicvoidpush(int x){ queue2.offer(x); while(!queue1.isEmpty()){ queue2.offer(queue1.poll()); } Queue temp = queue1; queue1 = queue2; queue2 = temp; } /** Removes the element on top of the stack and returns that element. */ publicintpop(){ return queue1.poll(); //queue1此时就是按照栈的顺序来存储元素的,所以直接poll即可 } /** Get the top element. */ publicinttop(){ return queue1.peek(); } /** Returns whether the stack is empty. */ publicbooleanempty(){ return queue1.isEmpty(); } }
/** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */
classMyStack{ Queue<Integer> queue; /** Initialize your data structure here. */ publicMyStack(){ queue = new LinkedList<Integer>(); } /** Push element x onto stack. */ publicvoidpush(int x){ int n = queue.size(); queue.offer(x); for(int i=0;i<n;i++){ queue.offer(queue.poll()); } } /** Removes the element on top of the stack and returns that element. */ publicintpop(){ return queue.poll(); } /** Get the top element. */ publicinttop(){ return queue.peek(); } /** Returns whether the stack is empty. */ publicbooleanempty(){ return queue.isEmpty(); } }
/** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */