File tree Expand file tree Collapse file tree 1 file changed +18
-16
lines changed Expand file tree Collapse file tree 1 file changed +18
-16
lines changed Original file line number Diff line number Diff line change 1
1
# Stack: Last In - First Out (LIFO)
2
2
3
+
3
4
# Define node format: value and a pointier to next
4
5
class Node :
5
6
def __init__ (self , value : int ) -> None :
@@ -10,48 +11,49 @@ def __init__(self, value: int) -> None:
10
11
class DynamicStack :
11
12
def __init__ (self ) -> None :
12
13
self .__first = None
13
-
14
+
14
15
def isEmpty (self ) -> bool :
15
16
if not self .__first :
16
17
return True
17
18
return False
18
-
19
- def push (self ,value : int ):
19
+
20
+ def push (self , value : int ):
20
21
node = Node (value )
21
22
node .next = self .__first
22
23
self .__first = node
23
24
24
25
def pop (self ) -> int :
25
26
if self .isEmpty ():
26
- raise Exception (' Stack is empty!' )
27
+ raise Exception (" Stack is empty!" )
27
28
removedElement = self .__first .value
28
29
self .__first = self .__first .next
29
30
return removedElement
30
-
31
31
32
32
def show (self ) -> None :
33
33
node = self .__first
34
- print ('[' , end = ' ' )
35
- while ( node ) :
36
- print (node .value , end = ' ' )
34
+ print ("[" , end = " " )
35
+ while node :
36
+ print (node .value , end = " " )
37
37
node = node .next
38
- print (']' )
38
+ print ("]" )
39
+
39
40
40
41
def main () -> None :
41
42
dynamic_stack = DynamicStack ()
42
43
43
- print (f' Push(1,2,4):' )
44
+ print (f" Push(1,2,4):" )
44
45
dynamic_stack .push (1 )
45
46
dynamic_stack .push (2 )
46
47
dynamic_stack .push (4 )
47
-
48
+
48
49
dynamic_stack .show ()
49
50
50
- print (f' Remove last: { dynamic_stack .pop ()} ' )
51
- print (f' Remove last: { dynamic_stack .pop ()} ' )
52
- print (f' Remove last: { dynamic_stack .pop ()} ' )
53
- print (' Empty stack!' )
51
+ print (f" Remove last: { dynamic_stack .pop ()} " )
52
+ print (f" Remove last: { dynamic_stack .pop ()} " )
53
+ print (f" Remove last: { dynamic_stack .pop ()} " )
54
+ print (" Empty stack!" )
54
55
dynamic_stack .show ()
55
56
57
+
56
58
if __name__ == "__main__" :
57
- main ()
59
+ main ()
You can’t perform that action at this time.
0 commit comments