# 栈和队列

1.数组结构实现较容易
2.用链表结构较复杂

1.pop操作
2.top或peek操作
3.push操作
4.size操作

### DFS 和 BFS

DFS可以用栈来实现，BFS可以用队列来实现。

### 可查询最值的栈

# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.stack = []
self.cmp = []

def push(self, node):
self.stack.append(node)
if not self.cmp:
self.cmp.append(node)
else:
if node < self.cmp[-1]:
self.cmp.append(node)
else:
self.cmp.append(self.cmp[-1])
def pop(self):
self.stack.pop()
return self.cmp.pop()

def top(self):
return self.stack[-1]

def min(self):
return self.cmp[-1]


### 双栈队列

[1,2,3,0,4,0],6

# -*- coding:utf-8 -*-

class TwoStack:

def __init__(self):
self.stackpop = []
self.stackpush = []

def twoStack(self, ope, n):
result = []
for x in ope:
if x > 0:
self.stackpush.append(x)
else:
if len(self.stackpop):
result.append(self.stackpop.pop())
else:
while len(self.stackpush):
self.stackpop.append(self.stackpush.pop())
result.append(self.stackpop.pop())
return result

if __name__ == '__main__':

sol = TwoStack()
ope = [2, 1, 1, 0, 3, 0]
n = 6
print sol.twoStack(ope, n)


### 栈的反转

[4,3,2,1],4

# -*- coding:utf-8 -*-

class StackReverse:
def reverseStack(self, A, n):
x=[]
for i in range(n):
x.append(A.pop())
return x


### 双栈排序

[1,2,3,4,5]

# -*- coding:utf-8 -*-
class TwoStacks:
def twoStacksSort(self, numbers):
return sorted(numbers , reverse=True)


### 滑动窗口

[4,3,5,4,3,3,6,7],8,3

# -*- coding:utf-8 -*-

class SlideWindow:
def slide(self, arr, n, w):
if w > n:
return False
list = []
for i in xrange(0,n-w+1):
list.append(max(arr[i:i+w]))
return list


### 数组变树

[3,1,4,2],4

todo