/** Initialize your data structure here. */ publicMyStack225() { list = new LinkedList(); } /** Push element x onto stack. */ publicvoidpush(int x){ list.add(x); } /** Removes the element on top of the stack and returns that element. */ publicintpop(){ int []tmp = newint[list.size()]; for (int i=tmp.length-1; i>=0; i--) tmp[i] = list.removeFirst(); int val = tmp[0]; if (tmp.length-1>=0) for (int i=tmp.length-1; i>0; i--) list.add(tmp[i]); return val; } /** Get the top element. */ publicinttop(){ int []tmp = newint[list.size()]; for (int i=tmp.length-1; i>=0; i--) tmp[i] = list.removeFirst(); int val = tmp[0]; for (int i=tmp.length-1; i>=0; i--) list.add(tmp[i]); return val; } /** Returns whether the stack is empty. */ publicbooleanempty(){ return list.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(); */
/** Initialize your data structure here. */ publicMyStack225() { list = new LinkedList(); } /** Push element x onto stack. */ publicvoidpush(int x){ int n = list.size(); list.add(x); for (int i=0; i<n; i++) list.add(list.poll()); } /** Removes the element on top of the stack and returns that element. */ publicintpop(){ return list.poll(); } /** Get the top element. */ publicinttop(){ return list.peek(); } /** Returns whether the stack is empty. */ publicbooleanempty(){ return list.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(); */