225. Use queues to implement stacks
In general, there are two methods:
两个
Realize with queue一个
Realize with queue
Method 1 : 两个
Realize with queue
The best way is to let the two queues q1
(in-stack queue) and q2
(out-stack queue) act as push-in and out-stack roles respectively, namely:
push
Add elements toq1
pop
Fromq2
thepoll
elements
q1
And q2
just a pointer, in push
the internal time by using a temporary variable to the exchange q1
and q2
the point, so that q1
and q2
point to always point to the queue 入栈队列
and 出栈队列
.
The advantage of this is that you can simplify pop()
the top()
implementation, as s2
is the stack queue, so only needs to be performed separately q2.poll()
and q2.peek()
can be.
The main reason is push()
that the implementation is more complicated. Here are a few pictures to illustrate the process of adding elements:
The Java implementation is as follows :
class MyStack {
private Queue<Integer> q1;
private Queue<Integer> q2;
public MyStack() {
q1 = new LinkedList<Integer>();
q2 = new LinkedList<Integer>();
}
// 只要是进栈操作就添加到q1,只要是出栈操作就从q2中poll
public void push(int x) {
q1.offer(x);
while(!q2.isEmpty()){
q1.offer(q2.poll());
}
// 通过一个临时的队列来交换q1和q2的指向
Queue<Integer> temp; // 这里只是声明一个变量,并没有开辟一个真正的队列空间
temp = q1;
q1 = q2;
q2 = temp;
}
public int pop() {
return q2.poll();
}
public int top() {
return q2.peek();
}
public boolean empty() {
return q1.isEmpty() && q2.isEmpty();
}
}
Time complexity analysis
- push()
Stack operation requires q2
the n
element of the team, the team incorporated n+1
elements to q1
total 2n+1
operations, each time a team into the team and the complexity of the operations are O(1)
, therefore stack operation time complexity O(n)
.
- The time complexity of a certain push depends on whether the pop queue is empty. If there is no pop operation (or the number of pops is much less than push), then the complexity of almost every push operation is O(n), because Almost every time q2 is not empty.
- The rest are
O(1)
Method 2 : 一个
Realize with queue
Similarly, when implemented with a queue, the main implementation lies in push()
operation.
The specific methods are as follows:
- Before adding an element each time, write down the number of elements in the current queue as
n
- Then execute to
queue.offer(x);
add the element toqueue
- The next more dreamy step is to put
queue
the firstn
elementpoll
inoffer
thequeue
- In this way, the current position at the top of the team must be newly added
x
Here are a few pictures to illustrate the process of adding elements:
The Java implementation is as follows :
class MyStack {
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<Integer>();
}
public void push(int x) {
int n = queue.size();
queue.offer(x);
for (int i = 0; i < n; i++) {
queue.offer(queue.poll());
}
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}
Time complexity analysis
- push()
Each push
operation will add n elements to it again queue
, so push()
the time complexity isO(n)
- The rest are
O(1)