두 큐는 스택, 두 개의 스택 큐 구현

1 개의 스택 큐를 구현

세 가지 아이디어가 있습니다 :

생각 : 팀이 stack1 요소가 stack2로 누른 스택은 스택의 상단과 다음 팝업 stack2를 설정하는 저장 공간, 임시 버퍼로 stack2 직접 stac1에 팀, 같은 stack1 마지막 stack2은 다음 요소에 stack1 되감기

이 생각 : 팀으로, 빈 stack1 모든 요소는, 모든에 stack1의 stack2을 부어 것처럼 stack1이 비어 있는지 여부를 확인하려면, 그렇지 않으면, 직접의 stack1에 요소 후 직접 stack1에,
팀, , stack2이 비어있는 경우 stack2이 비어 있는지 여부를 결정 쏟아 stack2 요소, 팝업에서 상위 요소 stack2, 또는 직접 팝업 스택 요소 stack2 stack1
생각 세 : 경우에 직접 팀 팀이 stack2이 비어있는 경우 stack2이 비어 있는지 여부를 결정 stack1는 stack1 요소는 팝업 요소 stack2의에 달리 직접 stack2을 부어
가 연속 팝 작업 또는 경우, 두 개의 아이디어 비교의 아이디어 연속 누름 동작, 생각 이상의 아이디어 훨씬 더. 바람직하게는 세 가지 아이디어 코드를 다음과 같습니다.

Q 스택 구현

 

# 两个栈实现一个队列
# 栈先近后
# 列表的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, 두 개의 큐는 스택을 구현하는

중계 지점으로 스택 queue2로 스택에 queue1을

경우 queue1을 직접 적층 압력

큐 queue2으로 누르면 큐의 마지막 요소를 제외하고 시퀀스에서 팝 queue1을 첫번째 요소 및 queue1을는 queue2에서 마지막 큐의 마지막 요소에 남아 스택의 요소 인 것 queue1을 내로 다시 가압 요소는
예시 :

Q 실현 스택

다음 코드는 다음과 같습니다

# 两个队列实现一个栈
# 队列就是先进先出 使用列表的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())

세 가지 아이디어가 있습니다 :

생각 : 팀이 stack1 요소가 stack2로 누른 스택은 스택의 상단과 다음 팝업 stack2를 설정하는 저장 공간, 임시 버퍼로 stack2 직접 stac1에 팀, 같은 stack1 마지막 stack2은 다음 요소에 stack1 되감기

이 생각 : 팀으로, 빈 stack1 모든 요소는, 모든에 stack1의 stack2을 부어 것처럼 stack1이 비어 있는지 여부를 확인하려면, 그렇지 않으면, 직접의 stack1에 요소 후 직접 stack1에,
팀, , stack2이 비어있는 경우 stack2이 비어 있는지 여부를 결정 쏟아 stack2 요소, 팝업에서 상위 요소 stack2, 또는 직접 팝업 스택 요소 stack2 stack1
생각 세 : 경우에 직접 팀 팀이 stack2이 비어있는 경우 stack2이 비어 있는지 여부를 결정 stack1는 stack1 요소는 팝업 요소 stack2의에 달리 직접 stack2을 부어
가 연속 팝 작업 또는 경우, 두 개의 아이디어 비교의 아이디어 연속 누름 동작, 생각 이상의 아이디어 훨씬 더. 바람직하게는 세 가지 아이디어 코드를 다음과 같습니다.

Q 스택 구현

 

# 两个栈实现一个队列
# 栈先近后
# 列表的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, 두 개의 큐는 스택을 구현하는

중계 지점으로 스택 queue2로 스택에 queue1을

경우 queue1을 직접 적층 압력

큐 queue2으로 누르면 큐의 마지막 요소를 제외하고 시퀀스에서 팝 queue1을 첫번째 요소 및 queue1을는 queue2에서 마지막 큐의 마지막 요소에 남아 스택의 요소 인 것 queue1을 내로 다시 가압 요소는
예시 :

Q 실현 스택

다음 코드는 다음과 같습니다

# 两个队列实现一个栈
# 队列就是先进先出 使用列表的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())

추천

출처www.cnblogs.com/ellisonzhang/p/11277824.html