forked from WenkeZhou/PythonDataStructure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLKStack.py
More file actions
68 lines (55 loc) · 1.4 KB
/
LKStack.py
File metadata and controls
68 lines (55 loc) · 1.4 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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'MrHero'
class Node(object):
# 节点
def __init__(self, data=None):
self.data = data
self.next = None
class LKStack(object):
def __init__(self):
self.top = Node(None)
self.count = 0
def get_length(self):
return self.count
def get_top(self):
# 返回栈顶元素
return self.top.data
def is_empty(self):
return self.count == 0
def push(self, elem):
# 进栈
tmp = Node(elem)
if self.is_empty():
self.top = tmp
else:
tmp.next = self.top
self.top = tmp
self.count += 1
def pop(self):
# 出栈
if self.is_empty():
raise IndexError("Stack is empty!")
else:
self.count -= 1
elem = self.top.data
self.top = self.top.next
return elem
def show_stack(self):
# 从栈顶开始显示各节点值
if self.is_empty():
raise IndexError("Stack is empty!")
else:
j = self.count
tmp = self.top
while j > 0 and tmp:
print tmp.data
tmp = tmp.next
j -= 1
if __name__ == '__main__':
lks = LKStack()
for i in range(1, 5):
lks.push(i)
lks.show_stack()
lks.pop()
lks.show_stack()