您的位置:首页 > 其它

用队列实现stack

2015-06-13 06:59 225 查看
用queue实现stack

题目要求: Implement the following operations

push(x) -- Push element x onto stack.

pop() -- Removes the element on top of the stack.

top() -- Get the top element.

empty() -- Return whether the stack is empty.

其中 use only standard operations of a queue -- which means only
push to back
,
peek/pop from front
,
size
, and
is empty
operations are valid.

Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.

You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

方法1:用两个queue

用两个queue的话很容易实现,类似于用stack实现queue的思路。其中一个queue主要用于改变element顺序时,临时存储element。

Solution 1 : making pop operation costly

pop和top时,因为需要取最后一个,需要把q中前面的移动到q2中。

In push operation, the new element is always enqueued to q1.
In pop() operation, if q2 is empty then all the elements except the last, are moved to q2.
Finally the last element is dequeued from q1 and returned.

push(s, x)
1) Enqueue x to q1 (assuming size of q1 is unlimited).

pop(s)
1) One by one dequeue everything except the last element from q1 and enqueue to q2.
2) Dequeue the last item of q1, the dequeued item is result, store it.
3) Swap the names of q1 and q2
4) Return the item stored in step 2.
// Swapping of names is done to avoid one more movement of all elements from q2 to q1.

class MyStack {
//update when push

Queue<Integer> q = new LinkedList<Integer>();
Queue<Integer> q2 = new LinkedList<Integer>();

// Push element x onto stack.
public void push(int x) {
q2.offer(x);
while(!q.isEmpty()){
q2.offer(q.poll());
}
//swap q and q2 name or push back to q
while(!q2.isEmpty()){
q.offer(q2.poll());
}
}

// Removes the element on top of the stack.
public void pop() {
q.poll();
}

// Get the top element.
public int top() {
return q.peek();
}

// Return whether the stack is empty.
public boolean empty() {
return q.isEmpty();
}
}


View Code
方法2:使用一个queue

在方法1中的push costly方法的基础上优化,减少空间

class MyStack {
//update when push

Queue<Integer> q = new LinkedList<Integer>();

// Push element x onto stack.
public void push(int x) {
q.offer(x);
int size = q.size();
while(size > 1){
q.offer(q.poll());
size--;
}

}

// Removes the element on top of the stack.
public void pop() {
q.poll();
}

// Get the top element.
public int top() {
return q.peek();
}

// Return whether the stack is empty.
public boolean empty() {
return q.isEmpty();
}
}


在pop and top costly 方法基础上优化,用一个元素mark peek即可

class MyStack {
//update when pop and top operation
//use 1 queue
Queue<Integer> q = new LinkedList<Integer>();

private Integer topElement = null;

// Push element x onto stack.
public void push(int x) {
topElement = x;
q.offer(x);
}

// Removes the element on top of the stack.
public void pop() {
int size = q.size();
Integer second = null;
while(size > 1){//all element to q2 except last element
second = q.poll();
q.offer(second);
size--;
}
topElement = second;//the second last element
q.poll();//pop the last element

}

// Get the top element.
public int top() {
return topElement;
}

// Return whether the stack is empty.
public boolean empty() {
return q.isEmpty();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: