-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlinked_list.py
More file actions
158 lines (115 loc) · 3.24 KB
/
Copy pathlinked_list.py
File metadata and controls
158 lines (115 loc) · 3.24 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
class ListNode:
def __init__(self, data):
# initialize this object
# store data
self.data = data
# store the reference (next item)
self.next = None
return
def has_value(self, value):
# method to compare the value with the node data
if self.data == value:
return True
else:
return False
node1 = ListNode(15)
#print(type(node1)) # <class '__main__.ListNode'>
class SingleLinkedList:
def __init__(self):
# initialize this object
self.head = None
self.tail = None
return
def add_list_item(self, item):
# make sure item is a proper node
if not isinstance(item, ListNode):
item = ListNode(item)
if self.head is None:
self.head = item
else:
self.tail.next = item
self.tail = item
return
def list_length(self):
#returns the number of list items
count = 0
current_node = self.head
while current_node is not None:
count = count + 1
# jump to the linked node
current_node = current_node.next
return count
def output_list(self):
# outputs the list (the value of the node, actually)
current_node = self.head
results = []
while current_node is not None:
results.append(current_node.data)
# jump to the linked node
current_node = current_node.next
print(results)
return
def unordered_search(self, value):
# search the linked list for the node that has this value
# define current_node
current_node = self.head
# define position
node_id = 1
# define list of results
results = []
while current_node is not None:
if current_node.data == value:
results.append(node_id)
node_id = node_id + 1
current_node = current_node.next
return results
def remove_list_item_by_id(self, id):
# remove the list item with the item id
current_id = 1
current_node = self.head
previous_node = None
while current_node is not None:
if current_id == id:
if previous_node is not None:
previous_node.next = current_node.next
else:
self.head = current_node.next
previous_node = current_node
current_node = current_node.next
current_id = current_id + 1
return
def reverse(self):
# reverse the order of the list
previous_node = None
current_node = self.head
next_node = current_node.next
while next_node is not None:
current_node.next = previous_node
previous_node = current_node
current_node = next_node
next_node = next_node.next
current_node.next = previous_node
self.head = current_node
return
def remove_value(self, value):
#remove the first item in the list with this value
previous_node = None
current_node = self.head
while current_node is not None:
if current_node.data == value:
previous_node.next = current_node.next
return
previous_node = current_node
current_node = current_node.next
return
"""
list1 = SingleLinkedList()
list1.add_list_item(node1)
list1.add_list_item('12')
list1.add_list_item(15)
list1.add_list_item(17)
list1.remove_list_item_by_id(3)
list1.reverse()
list1.remove_value(15)
list1.output_list()
"""