Python写数据结构:栈(顺序存储结构)
2018-02-01 20:27
204 查看
#!/usr/bin/python3.5 #_*_coding:utf-8_*_ class Stack(): def __init__(self,capacity): self.stack = [None] * capacity self.capacity = capacity self.top = -1 def push(self,element): if self.full(): print('栈满') return self.top += 1 self.stack[self.top] = element def pop(self): if self.empty(): print('栈是空的') return element = self.stack[self.top] self.stack[self.top] = None self.top -= 1 return element def empty(self): return self.top == -1 def full(self): return self.top == self.capacity - 1 def length(self): return self.top + 1 def getTop(self): if self.top == -1: print('栈是空的') return return self.stack[self.top] def clear(self): while self.top != -1: self.stack[self.top] = None self.top -= 1 if __name__ == '__main__': stack = Stack(10)
相关文章推荐
- python数据结构之线性表的顺序存储结构
- Python 数据结构 之 串 的顺序存储结构
- Python 数据结构 之 线性表 的顺序存储结构
- Python数据结构
- 【数据结构&&算法系列】插入排序简单介绍及python代码
- Python数据结构之映射(內建字典)
- Python实现基本线性数据结构
- 代写 程序 C C++ Java Python VC VB 数据结构 留学生作业 毕业设计
- python数据结构学习笔记-2016-11-07-03-多重链表以及相应的迭代器
- Python数据结构——树的实现
- python中三种特殊数据结构:set集合、堆、双端队列
- Python基础学习之基本数据结构Dict
- [Python]入门教程(五):Python中的数据结构
- 数据结构---线性表----单链表结构与顺序存储结构的对比
- Python学习(四)数据结构 —— str
- Python高级数据结构之Collection
- Python数据分析——Pandas数据结构和操作
- python数据结构学习笔记-2016-11-24-02-基数排序
- python 下的数据结构与算法---4:线形数据结构,栈,队列,双端队列,列表
- python数据结构之链表详解