-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsingly_linked.py
81 lines (66 loc) · 2.09 KB
/
singly_linked.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
class Node:
"""a node representation in linked list"""
def __init__(self, data, next_=None):
self.data = data
self.next = next_
def __str__(self):
"""return node data"""
return str(self.data)
class LinkedList:
"""simple linked list representation"""
def __init__(self):
self.head = None
def add(self, data):
""" add new element into linked list """
# create new node
node = Node(data, self.head)
# set head as new node
self.head = node
def search(self, value):
"""
Search linked list and return a Node with given value
otherwise return None
"""
current = self.head
while current is not None:
if current.data == value:
return current
current = current.next
return None
def remove(self, value):
""" Delete Node from linked list with given value """
current = self.head
previous = None
while current is not None and current.data != value:
previous = current
current = current.next
if previous is None:
self.head = current.next
elif current is not None:
# connect previous Node to next Node
previous.next = current.next
def __str__(self):
""" Presented linked list objects as string
Node.data -> NextNode.data
"""
string = ''
current = self.head
while current.next is not None:
string += str(current.data) + ' -> '
current = current.next
string += str(current.data) + ' -> ' + str(None)
return string
if __name__ in "__main__":
print('create initial linked list')
lst = LinkedList()
for i in [1, 7, 2, 3, -10]:
print('add {}'.format(i))
lst.add(i)
print()
print('linked list:', lst, '\n')
for i in [2, 4, -10]:
lst.remove(i)
print('remove {}'.format(i))
print('linked list:', lst, '\n')
for i in [3, 10]:
print('search {}'.format(i), lst.search(i))