/** Initialize your data structure here. */ publicMyStack() { queue1 = newLinkedList<Integer>(); queue2 = newLinkedList<Integer>(); } /** Push element x onto stack. */ publicvoidpush(int x) { queue2.offer(x); while (!queue1.isEmpty()) { queue2.offer(queue1.poll()); } Queue<Integer> temp = queue1; queue1 = queue2; queue2 = temp; } /** Removes the element on top of the stack and returns that element. */ publicintpop() { return queue1.poll(); } /** Get the top element. */ publicinttop() { return queue1.peek(); } /** Returns whether the stack is empty. */ publicbooleanempty() { return queue1.isEmpty(); } }
/** Initialize your data structure here. */ MyStack() {
}
/** Push element x onto stack. */ voidpush(int x){ queue2.push(x); while (!queue1.empty()) { queue2.push(queue1.front()); queue1.pop(); } swap(queue1, queue2); } /** Removes the element on top of the stack and returns that element. */ intpop(){ int r = queue1.front(); queue1.pop(); return r; } /** Get the top element. */ inttop(){ int r = queue1.front(); return r; } /** Returns whether the stack is empty. */ boolempty(){ return queue1.empty(); } };
/** Removes the element on top of the stack and returns that element. */ func(s *MyStack) Pop() int { v := s.queue1[0] s.queue1 = s.queue1[1:] return v }
/** Get the top element. */ func(s *MyStack) Top() int { return s.queue1[0] }
/** Returns whether the stack is empty. */ func(s *MyStack) Empty() bool { returnlen(s.queue1) == 0 }
/** Initialize your data structure here. */ publicMyStack() { queue = newLinkedList<Integer>(); } /** Push element x onto stack. */ publicvoidpush(int x) { intn= queue.size(); queue.offer(x); for (inti=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(); } }
/** Initialize your data structure here. */ MyStack() {
}
/** Push element x onto stack. */ voidpush(int x){ int n = q.size(); q.push(x); for (int i = 0; i < n; i++) { q.push(q.front()); q.pop(); } } /** Removes the element on top of the stack and returns that element. */ intpop(){ int r = q.front(); q.pop(); return r; } /** Get the top element. */ inttop(){ int r = q.front(); return r; } /** Returns whether the stack is empty. */ boolempty(){ return q.empty(); } };
def__init__(self): """ Initialize your data structure here. """ self.queue = collections.deque()
defpush(self, x: int) -> None: """ Push element x onto stack. """ n = len(self.queue) self.queue.append(x) for _ inrange(n): self.queue.append(self.queue.popleft())
defpop(self) -> int: """ Removes the element on top of the stack and returns that element. """ return self.queue.popleft()
deftop(self) -> int: """ Get the top element. """ return self.queue[0]
defempty(self) -> bool: """ Returns whether the stack is empty. """ returnnot self.queue