栈(LIFO)
class Stack(object):
"""
使用 python 实现栈
栈是一种LIFO(Last In First Out) 的数据结构,可以封装一个list实现
"""
def __init__(self):
self.__stack = list()
def push(self, value):
"""
append 到列表尾部,复杂度 O(1)
:param value:
:return:
"""
self.__stack.append(value)
def pop(self):
"""
pop 弹出
:return:
"""
return self.__stack.pop()
def isEmpty(self):
"""
:return:
"""
return len(self.__stack) == 0
def size(self):
"""
:return:
"""
return len(self.__stack)
if __name__ == "__main__":
stack = Stack()
stack.push(0)
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.size())
print(stack.pop())
队列(FIFO)
class Queue(object):
def __init__(self):
self.__queue = list()
def push(self, value):
self.__queue.append(value)
def pop(self):
return self.__queue.pop(0)
def isEmpty(self):
return len(self.__queue) == 0
def size(self):
return len(self.__queue)
if __name__ == "__main__":
q = Queue()
q.push(0)
q.push(1)
q.push(2)
print(q.pop())
print(q.pop())
print(q.pop())