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语言进阶知识点总结

数据结构和算法 算法:解决问题的方法和步骤 评价算法的好坏:渐近时间复杂度和渐近空间复杂度。 渐近时间复杂度的大O标记: - 常量时间复杂度 - 布隆过滤器 / 哈希存储 - 对数时间复...

Django使用Celery异步任务队列的使用

Django使用Celery异步任务队列的使用

1 Celery简介 Celery是异步任务队列,可以独立于主进程运行,在主进程退出后,也不影响队列中的任务执行。 任务执行异常退出,重新启动后,会继续执行队列中的其他任务,同...

python时间序列按频率生成日期的方法

有时候我们的数据是按某个频率收集的,比如每日、每月、每15分钟,那么我们怎么产生对应频率的索引呢?pandas中的date_range可用于生成指定长度的DatetimeIndex。 我...

解决pycharm的Python console不能调试当前程序的问题

使用python时,程序能运行,但是不能调试,找了半天解决方法,最后此操作分分钟奏效。 两种方法: 方法一:选中要运行的代码,右键Execute Selection in Console...

Python读csv文件去掉一列后再写入新的文件实例

用了两种方式解决该问题,都是网上现有的解决方案。 场景说明: 有一个数据文件,以文本方式保存,现在有三列user_id,plan_id,mobile_id。目标是得到新文件只有mobil...