-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstack.py
More file actions
90 lines (72 loc) · 1.59 KB
/
stack.py
File metadata and controls
90 lines (72 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Stack:
def __init__(self, size):
self.top = 0
self.size = size
self.data = [None] * size
# 打印栈中的所有元素
def print_stack(self):
if self.is_empty():
print("栈为空")
return
for i in range(self.top):
print(self.data[i], end=" ")
print()
# 判断栈是否已满
def is_full(self):
return self.top == self.size
# 判断栈是否为空
def is_empty(self):
return self.top == 0
# 入栈操作
def push(self, value):
if self.is_full():
return False
self.data[self.top] = value
self.top += 1
return True
# 出栈操作
def pop(self):
if self.is_empty():
return 0
self.top -= 1
return self.data[self.top]
# 清空栈
def make_empty(self):
self.top = 0
# 获取栈中元素的个数
def len(self):
return self.top
# 测试栈功能
def test_stack():
s = Stack(5)
print("执行入栈操作")
print(f"入栈 1: {s.push(1)}")
s.print_stack()
print(f"入栈 2: {s.push(2)}")
s.print_stack()
print("执行出栈操作")
print(f"出栈: {s.pop()}")
s.print_stack()
print(f"出栈: {s.pop()}")
s.print_stack()
print("清空栈")
s.make_empty()
s.print_stack()
print("测试结束")
test_stack()
"""
jarry@MacBook-Pro stack % python stack.py
执行入栈操作
入栈 1: True
1
入栈 2: True
1 2
执行出栈操作
出栈: 2
1
出栈: 1
栈为空
清空栈
栈为空
测试结束
"""