python学习数据结构实例代码

yipeiwu_com5年前Python基础

在学习python的过程中,用来练习代码,并且复习数据结构的

#coding:utf-8
#author:Elvis
 
class Stack(object):
 
  def __init__(self, size=8):
    self.stack = []
    self.size = size
    self.top = -1
 
  def is_empty(self):
    if self.top == -1:
      return True
    else:
      return False
 
  def is_full(self):
    if self.top +1 == self.size:
      return True
    else:
      return False
 
  def push(self, data):
    if self.is_full():
      raise Exception('stackOverFlow')
    else:
      self.top += 1
      self.stack.append(data)
 
  def stack_pop(self):
    if self.is_empty():
      raise Exception('stackIsEmpty')
    else:
      self.top -= 1
      return self.stack.pop()
 
 
  def stack_top(self):
    if self.is_empty():
      raise Exception('stackIsEmpty')
    else:
      return self.stack[self.top]
 
  def show(self):
    print self.stack
 
stack = Stack()
stack.push(1)
stack.push(2)
stack.push('a')
stack.push('b')
stack.push(5)
stack.push(6)
stack.stack_pop()
stack.stack_pop()
stack.stack_top()
stack.is_empty()
stack.is_full()
stack.show()

以上所述就是本文给大家分享的全部内容了,希望大家能够喜欢。

相关文章

Python中类的定义、继承及使用对象实例详解

本文实例讲述了Python中类的定义、继承及使用对象的方法。分享给大家供大家参考。具体分析如下: Python编程中类的概念可以比作是某种类型集合的描述,如“人类”可以被看作一个类,然后...

python映射列表实例分析

本文实例讲述了python映射列表。分享给大家供大家参考。具体分析如下: 列表映射是个非常有用的方法,通过对列表的每个元素应用一个函数来转换数据,可以使用一种策略或者方法来遍历计算每个元...

python实现监控windows服务并自动启动服务示例

使用Python 2.7 + pywin32 + wxpython开发 每隔一段时间检测一下服务是否停止,如果停止尝试启动服务。进行服务停止日志记录 AppMain.py 复制代码 代码...

详解python破解zip文件密码的方法

详解python破解zip文件密码的方法

1、单线程破解纯数字密码 注意: 不包括数字0开头的密码 import zipfile,time,sys start_time = time.time() def extract()...

Python简单I/O操作示例

本文实例讲述了Python简单I/O操作。分享给大家供大家参考,具体如下: 文件: poem = ''' hello world ''' f = file('book.txt', '...