1
1
# Program to print the elements of a linked list in reverse
2
-
3
2
from typing import List
4
3
5
4
class Node :
@@ -20,22 +19,18 @@ def __repr__(self):
20
19
21
20
def make_linked_list (elements_list : List ):
22
21
"""Creates a Linked List from the elements of the given sequence
23
- (list/tuple) and returns the head of the Linked List."""
24
- """
25
- >>> make_linked_list()
26
- Traceback (most recent call last):
27
- ...
28
- Exception: The Elements List is empty
22
+ (list/tuple) and returns the head of the Linked List.
23
+
29
24
>>> make_linked_list([])
30
25
Traceback (most recent call last):
31
26
...
32
27
Exception: The Elements List is empty
33
28
>>> make_linked_list([7])
34
- ' <7> ---> <END>'
29
+ <7> ---> <END>
35
30
>>> make_linked_list(['abc'])
36
- ' <abc> ---> <END>'
31
+ <abc> ---> <END>
37
32
>>> make_linked_list([7, 25])
38
- ' <7> ---> <25> ---> <END>'
33
+ <7> ---> <25> ---> <END>
39
34
"""
40
35
41
36
# if elements_list is empty
@@ -55,12 +50,8 @@ def make_linked_list(elements_list: List):
55
50
56
51
57
52
def print_reverse (head_node : Node ):
58
- """Prints the elements of the given Linked List in reverse order"""
59
- """
60
- >>> print_reverse()
61
- None
53
+ """Prints the elements of the given Linked List in reverse order
62
54
>>> print_reverse([])
63
- None
64
55
"""
65
56
66
57
# If reached end of the List
@@ -72,9 +63,27 @@ def print_reverse(head_node: Node):
72
63
print (head_node .data )
73
64
74
65
75
- list_data = [14 , 52 , 14 , 12 , 43 ]
76
- linked_list = make_linked_list (list_data )
77
- print ("Linked List:" )
78
- print (linked_list )
79
- print ("Elements in Reverse:" )
80
- print_reverse (linked_list )
66
+ def test_print_reverse_output ():
67
+ test_list_data = [69 , 88 , 73 ]
68
+ linked_list = make_linked_list (test_list_data )
69
+
70
+ print_reverse (linked_list )
71
+
72
+ def main ():
73
+ """
74
+ >>> test_print_reverse_output()
75
+ 73
76
+ 88
77
+ 69
78
+ """
79
+ list_data = [14 , 52 , 14 , 12 , 43 ]
80
+ linked_list = make_linked_list (list_data )
81
+ print ("Linked List:" )
82
+ print (linked_list )
83
+ print ("Elements in Reverse:" )
84
+ print_reverse (linked_list )
85
+
86
+
87
+ if __name__ == "__main__" :
88
+ main ()
89
+
0 commit comments