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