栈:先进后出
class Stack(object):
"""栈"""
def __init__(self):
self.__items = []
def is_empty(self):
return self.__items == []
def push(self,item):
self.__items.append(item)
def pop(self):
return self.__items.pop()
def peek(self):
'返回栈顶元素'
return self.__items[-1]
def size(self):
'返回栈大小'
return len(self.__items)
队列:前进先出
class Queue(object):
"""队列"""
def __init__(self):
self.__items = []
def is_empty(self):
return self.__items == []
def enqueue(self,item):
self.__items.insert(0, item)
#self.__items.append(item)
def dequeue(self):
return self.__items.pop()
# return self.__items.pop(0)
def size(self):
return len(self.__items)
class Deque(object):
"""双端队列"""
def __init__(self):
self.__items = []
def is_empty(self):
return self.__items == []
def add_front(item):
'从队头加入一个item元素'
self.__items.insert(0, item)
def add_rear(item):
'从队尾加入一个item元素'
self.__items.append(item)
def remove_front():
'从队头删除一个item元素'
return self.__items.pop(0)
def remove_rear():
'从队尾删除一个item元素'
return self.__items.pop()
def size(self):
return len(self.__items)