Two queues implemented stacks, two stacks queue

1, two stacks implement a queue

There are three ideas:

A thought: The stack1 as a storage space, the stack2 as a temporary buffer, when the team directly into the stac1, when the team will stack1 the elements turn the stack pressed into stack2, the top of the stack and then pop stack2 Finally, the stack2 the elements and then rewind to stack1

Thinking two: into the team, to determine whether stack1 is empty, as if all the elements stack1 empty, then poured stack1 stack2 in all, the elements and then directly into the stack1, otherwise, directly into the stack1 in
the team when, determine whether stack2 is empty, if stack2 is empty, stack1 the elements stack2 poured in, the top element stack2 in the pop-up, or directly pop stack elements stack2 the
idea three: when the team directly into the stack1 when the team determines whether stack2 is empty, if stack2 is empty, stack1 the elements poured stack2, otherwise directly in the pop-up element stack2
idea of a comparison with the two ideas, if it is continuous pop operations or continuous push operation, two ideas a lot better than thinking. Preferably the following three ideas codes.

Q stack implementation

 

# 两个栈实现一个队列
# 栈先近后
# 列表的append 和pop结合就是栈
class QueueWithTwoStack(object): def __init__(self): self._stack1 = [] self._stack2 = [] def pop(self): if self._stack2: return self._stack2.pop() elif not self._stack1: return None else: while self._stack1: self._stack2.append(self._stack1.pop()) return self._stack2.pop() def push(self, x): self._stack1.append(x) a = QueueWithTwoStack() for i in range(5): a.push(i) for i in range(5): print(a.pop()) 

2, two queues to implement a stack

The queue1 into the stack as the stack, queue2 as a transit point

When the stack, pressure directly into the queue1

When a pop queue1 first elements in sequence except for the last element of the queue, and pressed into the queue queue2, queue1 will remain in the last element of the queue is the element of the stack and, finally, in the queue2 again element pressed into queue1
illustrated:

Q realization stack

Codes are as follows:

# 两个队列实现一个栈
# 队列就是先进先出 使用列表的append() 和pop(0)实现
# 栈就是先进后出

class StackWithTwoQueue(object): def __init__(self): self._queue1 = [] # self._queue2 = [] def push(self, x): self._queue1.append(x) def pop(self): if not self._queue1: return None while self._queue1: if self._queue1.__len__() == 1: m = self._queue1.pop() break else: self._queue2.append(self._queue1.pop(0)) while self._queue2: self._queue1.append(self._queue2.pop(0)) return m b = StackWithTwoQueue() n = 3 for i in range(n): b.push(i) for i in range(n): print(b.pop())

There are three ideas:

A thought: The stack1 as a storage space, the stack2 as a temporary buffer, when the team directly into the stac1, when the team will stack1 the elements turn the stack pressed into stack2, the top of the stack and then pop stack2 Finally, the stack2 the elements and then rewind to stack1

Thinking two: into the team, to determine whether stack1 is empty, as if all the elements stack1 empty, then poured stack1 stack2 in all, the elements and then directly into the stack1, otherwise, directly into the stack1 in
the team when, determine whether stack2 is empty, if stack2 is empty, stack1 the elements stack2 poured in, the top element stack2 in the pop-up, or directly pop stack elements stack2 the
idea three: when the team directly into the stack1 when the team determines whether stack2 is empty, if stack2 is empty, stack1 the elements poured stack2, otherwise directly in the pop-up element stack2
idea of a comparison with the two ideas, if it is continuous pop operations or continuous push operation, two ideas a lot better than thinking. Preferably the following three ideas codes.

Q stack implementation

 

# 两个栈实现一个队列
# 栈先近后
# 列表的append 和pop结合就是栈
class QueueWithTwoStack(object): def __init__(self): self._stack1 = [] self._stack2 = [] def pop(self): if self._stack2: return self._stack2.pop() elif not self._stack1: return None else: while self._stack1: self._stack2.append(self._stack1.pop()) return self._stack2.pop() def push(self, x): self._stack1.append(x) a = QueueWithTwoStack() for i in range(5): a.push(i) for i in range(5): print(a.pop()) 

2, two queues to implement a stack

The queue1 into the stack as the stack, queue2 as a transit point

When the stack, pressure directly into the queue1

When a pop queue1 first elements in sequence except for the last element of the queue, and pressed into the queue queue2, queue1 will remain in the last element of the queue is the element of the stack and, finally, in the queue2 again element pressed into queue1
illustrated:

Q realization stack

Codes are as follows:

# 两个队列实现一个栈
# 队列就是先进先出 使用列表的append() 和pop(0)实现
# 栈就是先进后出

class StackWithTwoQueue(object): def __init__(self): self._queue1 = [] # self._queue2 = [] def push(self, x): self._queue1.append(x) def pop(self): if not self._queue1: return None while self._queue1: if self._queue1.__len__() == 1: m = self._queue1.pop() break else: self._queue2.append(self._queue1.pop(0)) while self._queue2: self._queue1.append(self._queue2.pop(0)) return m b = StackWithTwoQueue() n = 3 for i in range(n): b.push(i) for i in range(n): print(b.pop())

Guess you like

Origin www.cnblogs.com/ellisonzhang/p/11277824.html
Recommended