栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实现栈

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实现栈

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

class Stack():

 #初始化栈,并给定栈的大小

 def __init__(self,size):

  self.stack=[]

  self.size=size

  self.top=-1

 #判断栈是否满了,栈满返回True

 def Full(self):

  if self.top==(self.size-1):

   return True

  else:

   return False

 #判断栈是否为空,为空返回True

 def Empty(self):

  if self.top==-1:

   return True

  else:

   return False

 #入栈

 def stackin(self,content):

  if self.Full():

   print 'The stack is full!'

  else:

   self.stack.append(content)

   self.top+=1

 #出栈

 def stackout(self):

  if self.Empty():

   print 'The stack is empty!'

   return None

  else:

   content=self.stack[self.top]

   self.stack.pop(self.top)

   self.top-=1

   return content

 #遍历栈

 def stackall(self):

  if self.Empty():

   print 'The stack is Empty!'

  else:

   while self.top>=0:

    print self.stack[self.top]

    self.top-=1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/224278.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号