python学习数据结构实例代码 python学习数据结构实例代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python学习数据结构实例代码 python学习数据结构实例代码

  2021-03-21 我要评论
想了解python学习数据结构实例代码的相关内容吗,在本文为您仔细讲解python学习数据结构实例代码的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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()

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们