[Python数据结构] 使用List实现Stack

本文介绍如何使用Python的List数据结构来实现Stack(堆栈)抽象数据类型,包括push、pop、top、is_empty和is_full等核心方法。通过具体代码示例展示了堆栈的基本操作及其在实际应用中的执行流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[Python数据结构] 使用List实现Stack 

1. Stack 
堆栈(Stack)又称为栈或堆叠,是计算机科学中一种特殊的串列形式的抽象数据类型(ADT),其特殊之处在于只能允许在阵列的一端进行加入数据和删除数据,并且执行顺序应按照后进先出(LIFO)的原则。
堆栈[维基百科]

2. Stack ADT
堆栈是一种抽象数据类型,其实例S需要支持两种方法:
  1)S.push(e)  : add element e to the top of stack S
  2)S.pop( )  : remove and return the top element from the stack S

另外,为了方便使用,我们还定义了以下方法:
  3)S.top() : return the top element from the stack S, but not remove it
  4)S.is_empty() : Return True if the stack is empty
  5)len(S) : Return the number of elements in the stack

3. Implementing a Stack using a List

1 class Empty(Exception):
2     """Error attempting to access an element from an empty container"""
3     pass
4  
5 class OverFlow(Exception):
6     """Error attempting to push an element to an full container"""
7     pass
 1 class ArrayStack():
 2     """LIFO Stack implementation using a Python list as underlying storage"""
 3     
 4     def __init__(self, n):
 5         """Create an empty stack."""
 6         self.data = []
 7         self.maxLen = n  # n : an integer that represent the max elements capacity of the stack
 8  
 9     def __len__(self):
10         """Return the number of elements in the stack"""
11         return len(self.data)
12     
13     def is_empty(self):
14         """Return True if the stack is empty"""
15         return len(self.data) == 0 
16     
17     def is_full(self):
18         """Return True if the stack is full"""
19         return len(self.data) == self.maxLen
20     
21     def push(self, e):
22         """Add element e to the top of the stack
23         
24          Raise Empty exception if the stack is full"""
25         if self.is_full():
26             raise OverFlow('Stack is full')            
27         return self.data.append(e)
28     
29     def top(self):
30         """Return the element at the top of the stack, but not move it.
31         
32         Raise Empty exception if the stack is empty"""
33         if self.is_empty():
34             raise Empty('Stack is empty')
35         return self.data[-1]
36     
37     def pop(self):
38         """Return the element at the top of the stack, meanwhile move it.
39         
40         Raise Empty exception if the stack is empty"""
41         if self.is_empty():
42             raise Empty('Stack is empty')
43         return self.data.pop()

 

4. 执行结果:

1 s = ArrayStack(10)
2 l = np.random.randint(0, 10, size=(10, ))
3 for i in l:
4     s.push(i)
1 s.data
2 [6, 8, 7, 4, 2, 3, 4, 1, 5, 8]
1 s.pop()
2 8
1 s.data
2 [6, 8, 7, 4, 2, 3, 4, 1, 5]
1 s.top()
2 5
1 s.data
2 [6, 8, 7, 4, 2, 3, 4, 1, 5]

 

转载于:https://www.cnblogs.com/iwangzhengchao/p/9845355.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值