-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChainStack.py
More file actions
53 lines (43 loc) · 978 Bytes
/
ChainStack.py
File metadata and controls
53 lines (43 loc) · 978 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
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: UTF-8
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
class Stack(object):#链式栈
def __init__(self):
self.top = None
def is_empty(self):
if self.top is None:
print('This is an empty stack')
def push(self,value):
node = Node(value)
node.next = self.top
self.top = node
def pop(self):
node = self.top
if node is None:
print('This is an empty stack')
return
self.top = node.next
return node.data
def peek(self):
node = self.top
if node is None:
print('This is an empty stack')
return
return node.value
def size(self):
node = self.top
count = 0
if node is None:
raise Exception('This is an empty stack')
while node is not None:
count += 1
node = node.next
return count
if __name__ == '__main__':
stack = Stack()
stack.push(1)
print(stack.pop())
stack.push(2)
print(stack.pop())