-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab6.py
66 lines (55 loc) · 1.95 KB
/
lab6.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
# 8.12 LAB: Output a linked list
#
# Write a recursive function called print_list() that outputs the integer value of each node in a linked list. Function print_list() has one parameter, the head node of a list. The main program reads the size of the linked list, followed by the values in the list. Assume the linked list has at least 1 node.
#
# Ex: If the input of the program is:
#
# 5
# 1
# 2
# 3
# 4
# 5
#
# the output of the print_list() function is:
#
# 1, 2, 3, 4, 5,
#
# Hint: Output the value of the current node, then call the print_list() function repeatedly until the end of the list is reached. Refer to the Node class to explore any available instance methods that can be used for implementing the print_list() function.
class Node:
def __init__(self, value):
self.data_val = value
self.next_node = None
def insert_after(self, node):
tmp_node = self.next_node
self.next_node = node
node.next_node = tmp_node
def get_next(self):
return self.next_node
def print_data(self):
print(self.data_val, end=", ")
# Solution:
def print_list(head_node, current_node=None):
# initial setup for current node on first call
if current_node == None:
current_node = head_node
# base case, if we're out of nodes
if (current_node.get_next() == None):
current_node.print_data()
# recursive case
else:
current_node.print_data()
print_list(head_node, current_node.get_next())
# End Solution
if __name__ == "__main__":
size = int(input())
value = int(input())
head_node = Node(value) # Make head node as the first node
last_node = head_node
# Insert the second and the rest of the nodes
for n in range(1, size):
value = int(input())
new_node = Node(value)
last_node.insert_after(new_node)
last_node = new_node
print_list(head_node)