-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinked_lst.py
53 lines (41 loc) · 1004 Bytes
/
linked_lst.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
class Node(object):
def __init__(self, data=None, pointer=None):
self.__data = data
self.__pointer = pointer
def get_data(self):
return self.__data
def get_pointer(self):
return self.__pointer
def set_pointer(self, new_pointer):
self.__pointer = new_pointer
class LinkedList(object):
def __init__(self, root=None):
self.__root = root
def insert(self, data):
new_node = Node(data)
new_node.set_pointer(self.__root)
self.__root = new_node
@property
def size(self):
current = self.__root
count = 0
while current:
count += 1
current = current.get_pointer()
return count
def search(self, data):
current = self.__root
while current:
if current.get_data() == data:
return "item is in lst"
else:
current = current.get_pointer()
return "Item not in lst"
@property
def print_lst(self):
lst = []
current = self.__root
while current:
lst.append(str(current.get_data()))
current = current.get_pointer()
print( '--'.join(lst))