-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.py
70 lines (60 loc) · 1.83 KB
/
LinkedList.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
# creates Node class that will hold relevant data at each junction in the linked list
class Node:
#constructor
def __init__(self, vin):
self.vin = vin
self.setting = None
self.far = 0
self.medium = 0
self.near = 0
self.off = 0
self.speed1 = 0
self.speed2 = 0
self.speed3 = 0
self.next = None
def __repr__(self):
return self.vin
# creates Linked List class
class LinkedList:
# constructor
def __init__(self, nodes=None):
self.head = None
if nodes is not None:
node = Node(data=nodes.pop(0))
self.head = node
for elem in nodes:
node.next = Node(data=elem)
node = node.next
# allows us to print out the linked list and get a visual representation to visualize how it is functioning
def __repr__(self):
node = self.head
nodes = []
while node is not None:
nodes.append(node.vin)
node = node.next
nodes.append("None")
return " -> ".join(nodes)
# allows for iteration through the linked list (i.e. for node in llist)
def __iter__(self):
node = self.head
while node is not None:
yield node
node = node.next
# adds a new node to the front of the linked list
def add_first(self, node):
node.next = self.head
self.head = node
# removes a node based on the contents of the parameter 'target_node_data'
def remove_node(self, target_node_data):
if not self.head:
raise Exception("List is empty")
if self.head.vin == target_node_data:
self.head = self.head.next
return
previous_node = self.head
for node in self:
if node.vin == target_node_data:
previous_node.next = node.next
return
previous_node = node
raise Exception("Node with data '%s' not found" % target_node_data)