File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Node :
2
+ def __init__ (self , value ):
3
+ self .value = value
4
+ self .next = None
5
+
6
+ class Stack :
7
+ def __init__ (self ):
8
+ self .head = None
9
+ self .num_elements = 0
10
+ def to_list (self ):
11
+ py_list = []
12
+ node = self .head
13
+ while node :
14
+ py_list .append (node .value )
15
+ node = node .next
16
+ return py_list
17
+ def push (self , value ):
18
+ if self .head is None :
19
+ self .head = Node (value )
20
+ else :
21
+ new_node = Node (value )
22
+ new_node .next = self .head
23
+ self .head = new_node
24
+ self .num_elements += 1
25
+ def size (self ):
26
+ return self .num_elements
27
+ def is_empty (self ):
28
+ return self .num_elements == 0
29
+ def pop (self ):
30
+ if self .head is None :
31
+ return None
32
+ value = self .head .value
33
+ self .head = self .head .next
34
+ self .num_elements -= 1
35
+ return value
36
+
37
+
38
+ stack = Stack ()
39
+ stack .push (10 )
40
+ stack .push (20 )
41
+ stack .push (30 )
42
+ stack .push (40 )
43
+ stack .push (50 )
44
+
45
+ # Test size
46
+ print ("Pass" if (stack .size () == 5 ) else "Fail" )
47
+
48
+ # Test pop
49
+ print ("Pass" if (stack .pop () == 50 ) else "Fail" )
50
+
51
+ # Test push
52
+ stack .push (60 )
53
+ print ("Pass" if (stack .pop () == 60 ) else "Fail" )
54
+ print ("Pass" if (stack .pop () == 40 ) else "Fail" )
55
+ print ("Pass" if (stack .pop () == 30 ) else "Fail" )
56
+ stack .push (50 )
57
+ print ("Pass" if (stack .size () == 3 ) else "Fail" )
58
+
59
+
60
+
61
+
62
+
You can’t perform that action at this time.
0 commit comments