1.基于python列表实现栈:

#基于python的列表的栈; 后进先出

class  Myqueque():

	def __init__(self):

		self.qlist = []

	def push(self,arg):

		self.qlist.append(arg)

	def pop(self):
		#把列表反转一下就类似队列了。但是就没有hand主的效果
		# self.qlist.reverse()
		#有Hand主的效果了。
		while True:
			try:
				data = self.qlist.pop()
				if data:
					return data
			except Exception:
				import time
				time.sleep(5)


myque = Myqueque()
myque.push(1)
myque.push(2)
myque.push(4)
myque.push(7)
myque.push(7)
ret1 = myque.pop()
ret2 = myque.pop()
ret3 = myque.pop()
ret4 = myque.pop()
ret5 = myque.pop()
# print(ret1,ret2,ret3,ret4)

print(ret5)


基于redis列表的栈

import redis

conn = redis.Redis(host='127.0.0.1',port=6379,password='zh4350697')
class  Myqueque():

	def __init__(self):

		self.conn =redis.Redis(host='127.0.0.1',port=6379,password='zh4350697')

	def push(self,arg):

		self.conn.lpush("queque",arg)

	def pop(self):
		_,data = self.conn.blpop("queque")
		return data.decode('utf-8')

myque = Myqueque()
myque.push(1)
myque.push(2)
myque.push(4)
ret1 = myque.pop()
ret2 = myque.pop()
ret3 = myque.pop()
# ret4 = myque.pop()
print(ret1,ret2,ret3)
# print(ret4)

  

  

posted on 2019-10-11 11:44  小辉python  阅读(189)  评论(0编辑  收藏  举报