-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedList.py
More file actions
185 lines (152 loc) · 4.88 KB
/
LinkedList.py
File metadata and controls
185 lines (152 loc) · 4.88 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
class Node(object):
def __init__(self,val):
self.val = val
self.next = None
def get_data(self):
return self.val
def set_data(self,val):
self.val = val
def get_next(self):
return self.next
def set_next(self,next):
self.next = next
class LinkedList(object):
def __init__(self,*values):
self.count = len(values) -1
self.head = Node(values[0])
node = self.head
for idx, val in enumerate(values):
if idx == 0:
continue
else:
tempnode = Node(val)
node.set_next(tempnode)
node = node.get_next()
def get_count(self):
return self.head
def insert(self,data):
new_node = Node(data)
new_node.set_next(self.head)
self.head = new_node
self.count +=1
def insert_at(self,idx,val):
if idx > self.count +2:
return
if idx == 0:
self.insert(val)
else:
tempIdx = 0
node = self.head
while tempIdx < idx -1:
node = node.get_next()
tempIdx += 1
continuation = node.get_next()
insertion = Node(val)
node.set_next(insertion)
node.get_next().set_next(continuation)
self.count += 1
def find(self,val):
item = self.head
while item != None:
if item.get_data() == val:
return item
else:
item = item.get_next()
return None
def deleteAt(self,idx):
if idx > self.count+1:
return
if idx == 0:
self.head = self.head.get_next()
else:
tempIdx = 0
node = self.head
while tempIdx < idx -1:
node = node.get_next()
tempIdx +=1
node.set_next(node.get_next().get_next())
self.count -= 1
def dump_list(self):
tempnode = self.head
while (tempnode != None):
print("Node: ",tempnode.get_data())
tempnode = tempnode.get_next()
def swap(self,idx_a,idx_b):
if idx_a == idx_b:
return
elif idx_a > idx_b:
idx_2,idx_1 = idx_a,idx_b
else:
idx_2,idx_1 = idx_b,idx_a
node = self.head
tempIdx = 0
while tempIdx < idx_2:
if tempIdx != idx_1:
node = node.get_next()
tempIdx += 1
else:
elem_1 = node.get_data()
node = node.get_next()
tempIdx += 1
elem_2 = node.get_data()
self.deleteAt(idx_1)
self.deleteAt(idx_2-1)
self.insert_at(idx_1,elem_2)
self.insert_at(idx_2,elem_1)
def move_min(self,sorted_idx):
temp_idx = 0
node = self.head
selection = self.head.get_data()
while temp_idx <= self.count:
if temp_idx <= sorted_idx:
node = node.get_next()
temp_idx += 1
elif temp_idx == sorted_idx +1:
selection = node.get_data()
selected_idx = temp_idx
node = node.get_next()
temp_idx += 1
else:
if node.get_data() < selection:
selection = node.get_data()
selected_idx = temp_idx
try:
node = node.get_next()
temp_idx +=1
except:
break
self.deleteAt(selected_idx)
self.insert_at(sorted_idx, selection)
return sorted_idx + 1
def selection_sort(self):
"""
Note, move_min() method assumes that the element at first index is already sorted. As such, after
iteratively calling move_min(), the first element will be moved to the final index. Logic must be
built in to ID the final element and move it to its appropriate home.
"""
# part 1: sorts elements, pushing first element to last position
sorted_idx = 0
while sorted_idx < self.count:
sorted_idx = self.move_min(sorted_idx)
# part 2: identifies final element and relocates appropriately
temp_idx = 0
node = self.head
while temp_idx < self.count:
node = node.get_next()
temp_idx += 1
final_elem = node.get_data()
final_idx = temp_idx
temp_idx = 0
node = self.head
while temp_idx < self.count:
if node.get_data() < final_elem:
temp_idx += 1
node = node.get_next()
else:
self.deleteAt(final_idx)
self.insert_at(temp_idx,final_elem)
break
if __name__ == '__main__':
t1 = LinkedList(4,2,1,0,3)
t1.selection_sort()
t1.dump_list()