[Data Structure] Stack Implementation in Python
Posted chiyeung
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Data Structure] Stack Implementation in Python相关的知识,希望对你有一定的参考价值。
We can realize a Stack as an adaptation of a Python List.
S.push(e)=L.append(e)
S.pop()=L.pop()
S.top()=L[-1]
S.len()=len(L)
S.is_empty=(len(L)==0)
class Empty(Exception): pass class ArrayStack: """LIFO Stack implementation using Python""" def __init__(self): self._data=[] def __len__(self): return len(self._data) def is_empty(self): return len(self._data)==0 def push(self,e): self._data.append(e) def pop(self): if self.is_empty(): raise Empty(‘Stack is empty‘) return self._data.pop() def top(self): if self.is_empty(): raise Empty(‘Stack is empty‘) return self._data[-1]
以上是关于[Data Structure] Stack Implementation in Python的主要内容,如果未能解决你的问题,请参考以下文章
Data structure basics - Java Implementation
[LeetCode] 系统刷题8_Data Structure
Chapter eight Data Structure(数据结构)
Data Structure Interview Questions Data Structure Interview Questions