时间:2021-05-22
本文实例讲述了Python实现栈的方法。分享给大家供大家参考,具体如下:
使用Python 实现栈。
两种实现方式:
完整代码可见GitHub:
https://github.com/GYT0313/Python-DataStructure/tree/master/5-stack
目录结构:
注:一个完整的代码并不是使用一个py文件,而使用了多个文件通过继承方式实现。
arraycollection.py
"""File: abstractcollection.pyAuthor: Ken Lambert"""class AbstractCollection(object): """An abstract collection implementation.""" # Constructor def __init__(self, sourceCollection = None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present.""" self._size = 0 if sourceCollection: for item in sourceCollection: self.add(item) # Accessor methods def isEmpty(self): """Returns True if len(self) == 0, or False otherwise.""" return len(self) == 0 def __len__(self): """Returns the number of items in self.""" return self._size def __str__(self): """Returns the string representation of self.""" return "[" + ", ".join(map(str, self)) + "]" def __add__(self, other): """Returns a new bag containing the contents of self and other.""" result = type(self)(self) for item in other: result.add(item) return result def __eq__(self, other): """Returns True if self equals other, or False otherwise.""" if self is other: return True if type(self) != type(other) or \ len(self) != len(other): return False otherIter = iter(other) for item in self: if item != next(otherIter): return False return Trueabstractstack.py
"""File: abstractstack.pyAuthor: Ken Lambert"""from abstractcollection import AbstractCollectionclass AbstractStack(AbstractCollection): """An abstract stack implementation.""" # Constructor def __init__(self, sourceCollection = None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present.""" AbstractCollection.__init__(self, sourceCollection) # Mutator methods def add(self, item): """Adds item to self.""" self.push(item)运行示例:
代码:
栈实现:arraystack.py
数组实现:arrays.py
"""File: arrays.pyAn Array is a restricted list whose clients can useonly [], len, iter, and str.To instantiate, use<variable> = array(<capacity>, <optional fill value>)The fill value is None by default."""class Array(object): """Represents an array.""" def __init__(self, capacity, fillValue = None): """Capacity is the static size of the array. fillValue is placed at each position.""" self._items = list() for count in range(capacity): self._items.append(fillValue) def __len__(self): """-> The capacity of the array.""" return len(self._items) def __str__(self): """-> The string representation of the array.""" return str(self._items) def __iter__(self): """Supports iteration over a view of an array.""" return iter(self._items) def __getitem__(self, index): """Subscript operator for access at index.""" return self._items[index] def __setitem__(self, index, newItem): """Subscript operator for replacement at index.""" self._items[index] = newItem运行示例:
代码:
linkedstack.py
node.py
"""链表结构的节点类"""class Node(object): def __init__(self, data, next=None): self.data = data self.next = next参考:《数据结构(Python语言描述)》
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Python栈的实现方法。分享给大家供大家参考,具体如下:Python实现栈栈的数组实现:利用python列表方法代码如下:#列表实现栈,利用py
python系统调用的实例详解本文将通过两种方法对python系统调用进行讲解,包括python使用CreateProcess函数运行其他程序和ctypes模块
C语言实现单链表实现方法链表和我们之前实现过的顺序表一样,都是简单的数据结构,链表分为单向链表、双向链表、循环链表。而单向链表又分为两种实现方法,一种为带头节点
Java实现单链表反转,递归和非递归两种形式/***反转单链表*//***定义链表**@author16026**/classNode{intval;Noden
单链表的逆序输出分为两种情况,一种是只逆序输出,实际上不逆序;另一种是把链表逆序。本文就分别实例讲述一下两种方法。具体如下:1.逆序输出实例代码如下:#incl