-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlinked_stack.py
77 lines (62 loc) · 1.51 KB
/
linked_stack.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by imoyao at 2019/9/17 15:58
"""
基于链表实现的栈
"""
class Node:
def __init__(self, data, next_node=None):
self.data = data
self.next_node = next_node
class LinkedStack:
"""A stack based upon singly-linked list.
"""
def __init__(self):
self._head = None
self._size = 0
def __repr__(self):
current = self._head
nums = []
while current:
nums.append(current.data)
current = current.next_node
ret = ','.join([repr(num) for num in nums])
return f'[{ret}]'
def is_empty(self):
return self._size == 0
def size(self):
return self._size
def push(self, item):
new_head = Node(item)
new_head.next_node = self._head
self._head = new_head
self._size += 1
def pop(self):
if not self._size:
print('Stack Overflow')
else:
item = self._head.data
self._head = self._head.next_node
return item
def top(self):
"""
返回栈顶元素
:return:
"""
if not self._size:
print('Stack Overflow.')
else:
return self._head.data
if __name__ == '__main__':
s = LinkedStack()
print(s.is_empty())
s.push(4)
print(s)
s.push('dog')
print(s)
print(s.top())
s.push(True)
print(s)
print(s.size())
print(s.is_empty())
print(s.pop())