手写栈和队列

1、栈(特点:后进先出)

 1 # 栈:后进先出
 2 class Stack(object):
 3 
 4     def __init__(self):
 5         self.data = []
 6 
 7     def push(self, item):
 8         return self.data.append(item)
 9 
10     def pop(self):
11         return self.data.pop()
栈:后进先出

2、队列(特点:先进先出)

 1 # 队列:先进先出
 2 class Queue(object):
 3 
 4     def __init__(self):
 5         self.data = []
 6 
 7     def push(self, item):
 8         self.data.insert(0, item)
 9 
10     def pop(self):
11         return self.data.pop()
队列:先进先出

猜你喜欢

转载自www.cnblogs.com/Ming-Hui/p/9349487.html