-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedstack.py
More file actions
53 lines (38 loc) · 991 Bytes
/
linkedstack.py
File metadata and controls
53 lines (38 loc) · 991 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
class Node():
def __init__(self, value, link):
self.value = value
self.link = link
class LinkedStack():
def __init__(self):
self.__top = None
self.__length = 0
def push(self, e):
newNode = Node(e, self.__top)
self.__top = newNode
self.__length += 1
def pop(self):
result = self.__top.value
self.__top = self.__top.link
self.__length -= 1
return result
def top(self):
return self.__top.value
def length(self):
return self.__length
def print_stack(self):
current = self.__top
while current:
print(current.value)
current = current.link
def test_stack():
stack = LinkedStack()
stack.push(10)
stack.push(20)
stack.push(30)
stack.push(40)
assert stack.pop() == 40
assert stack.pop() == 30
print(stack.length())
print(stack.top())
stack.print_stack()
test_stack()