python学习数据结构实例代码

作者:hebedich 时间:2023-09-20 22:56:32 

在学习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,数据结构
0
投稿

猜你喜欢

  • Python输出由1,2,3,4组成的互不相同且无重复的三位数

    2021-07-26 10:54:42
  • Go语言实现ssh&scp的方法详解

    2024-04-29 13:05:31
  • Python Django Vue 项目创建过程详解

    2022-03-28 22:06:38
  • 简单了解SQL常用删除语句原理区别

    2024-01-14 22:38:57
  • Golang实现字符串倒序的几种解决方案

    2024-01-30 20:01:37
  • python中ndarray数组的索引和切片的使用

    2022-10-12 13:49:55
  • pandas数据预处理之dataframe的groupby操作方法

    2022-07-07 19:27:56
  • Flask中Cookie和Session理解与作用介绍

    2022-05-03 21:50:35
  • MySQL中事务概念的简洁学习教程

    2024-01-15 18:05:46
  • 一文带你了解MySQL四大类日志

    2024-01-25 12:53:13
  • selenium + python 获取table数据的示例讲解

    2021-02-20 11:57:46
  • python beautifulsoup4 模块详情

    2021-12-30 07:50:03
  • Appium+python自动化之连接模拟器并启动淘宝APP(超详解)

    2021-03-08 01:07:41
  • Yolov5训练意外中断后如何接续训练详解

    2022-05-14 00:16:51
  • asp下以Json获取中国天气网天气的代码

    2011-03-06 11:01:00
  • python实现的文件夹清理程序分享

    2021-07-20 07:58:57
  • python字符串判断密码强弱

    2021-05-09 04:20:04
  • Django前后端分离csrf token获取方式

    2021-03-15 04:23:43
  • 在Python中操作字符串之replace()方法的使用

    2021-03-15 14:10:59
  • python学习之matplotlib绘制散点图实例

    2021-02-22 21:28:22
  • asp之家 网络编程 m.aspxhome.com