forked from EINDEX/Python-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
40 lines (33 loc) · 957 Bytes
/
stack.py
File metadata and controls
40 lines (33 loc) · 957 Bytes
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
class MyStack(object):
def __init__(self, value=None):
self.value = value
# 前驱
self.before = None
# 后继
self.behind = None
def __str__(self):
if self.value is not None:
return str(self.value)
else:
return 'None'
def top(stack):
if isinstance(stack, MyStack):
if stack.behind is not None:
return top(stack.behind)
else:
return stack
def push(stack, ele):
push_ele = MyStack(ele)
if isinstance(stack, MyStack):
stack_top = top(stack)
push_ele.before = stack_top
push_ele.before.behind = push_ele
def pop(stack):
if isinstance(stack, MyStack):
stack_top = top(stack)
if stack_top.before is not None:
stack_top.before.behind = None
stack_top.behind = None
return stack_top
else:
print('已经是栈顶了')