-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100-singly_linked_list.py
executable file
·102 lines (81 loc) · 2.4 KB
/
100-singly_linked_list.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python3
"""100-singly_linked_list.py module
Raises:
TypeError: data must be an integer
Returns:
Create a single linked list
"""
class Node():
"""Linked list node
"""
def __init__(self, data, next_node=None):
"""Initialize variables
Arguments:
data -- data to be assigned to the node
Keyword Arguments:
next_node -- pointer to the next node (default: {None})
"""
self.data = data
self.next_node = next_node
@property
def data(self):
"""data attr getter
Returns:
returns the data attribute
"""
return self.__data
@data.setter
def data(self, value):
"""data attr setter
Arguments:
value -- value to be assigned to the data attribute
Raises:
TypeError: data must be an integer
"""
if type(value) is not int:
raise TypeError('data must be an integer')
self.__data = value
@property
def next_node(self):
"""next_node attr getter
Returns:
pointer to the next node
"""
return self.__next_node
@next_node.setter
def next_node(self, value):
"""next_node attr setter
Arguments:
value -- value to be assigned to the next_node attribute
Raises:
TypeError: next_node must be a Node object
"""
if value is not None and not isinstance(value, Node):
raise TypeError('next_node must be a Node object')
self.__next_node = value
class SinglyLinkedList():
"""Create a single linked list
"""
def __init__(self):
"""Initialize variables
"""
self.__head = None
def __str__(self):
"""Readable string representation for the class
Returns:
All the list
"""
self.__list = []
self.__tempNode = self.__head
while self.__tempNode != None:
self.__list.append(self.__tempNode.data)
print("print list: {}".format(self.__tempNode.data))
self.__list.append("\n")
self.__tempNode = self.__tempNode.next_node
return "".join(self.__list)
def sorted_insert(self, value):
"""insert value in its sorted place
Arguments:
value -- the value to be assigned to the new node
"""
pass