-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinked_list.py
176 lines (126 loc) · 3.81 KB
/
linked_list.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
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
import random
class Node:
data = None
next_node = None
def __init__(self,data):
self.data = data
def __repr__(self):
return f"<Node data: {self.data}>"
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def is_empty(self):
return self.head == None
def size(self):
current = self.head
count = 0
while current:
count += 1
current = current.next_node
return count
def search(self,key):
current = self.head
while current:
if current.data == key:
return current
current = current.next_node
return None
def add(self,data):
#Adds new node at tail
if self.head is None:
new_node = Node(data)
self.head = new_node
self.tail = new_node
else:
new_node = Node(data)
self.tail.next_node = new_node
self.tail = new_node
def add_to_head(self,data):
new_node = Node(data)
new_node.next_node = self.head
self.head = new_node
def insert(self,data,index):
if index == 0:
self.add = data
if index > 0:
new = Node(data)
position = index
current = self.head
while position > 1:
current = current.next_node
position -= 1
prev = current
next = current.next_node
prev.next_node = new
new.next_node = next
def remove(self,key):
current = self.head
previous = None
found = False
while current and not found:
if current.data == key and current is self.head:
found = True
self.head = current.next_node
elif current.data == key:
found = True
previous.next_node = current.next_node
previous = current
current = current.next_node
return current
def sort(self):
if self.head == None or self.head.next_node == None:
return self.head
mid = self.get_middle(self.head)
nex_to_mid = mid.next_node
mid.next_node = None
left = LinkedList()
right= LinkedList()
left.head = self.head
right.head = nex_to_mid
left.sort()
right.sort()
self.head = self.merge(left.head, right.head)
def merge(self,left,right):
if left == None:
return right
if right == None:
return left
if left.data <= right.data:
result = left
result.next_node = self.merge(left.next_node , right)
else:
result = right
result.next_node = self.merge(left, right.next_node)
return result
def get_middle(self, head):
if self.head == None:
return head
slow = head
fast = head.next_node
while fast.next_node and fast.next_node:
slow = slow.next_node
fast = fast.next_node
return slow
def __repr__(self):
nodes = []
current = self.head
while current:
if current is self.head:
nodes.append(f"[Head : {current.data}]")
elif current.next_node is None:
nodes.append(f"[Tail : {current.data}]")
else:
nodes.append(f"[{current.data}]")
current = current.next_node
return '->'.join(nodes)
def main():
l = LinkedList()
for i in range(0,15):
l.add(random.randint(0,40))
print(l)
l.sort()
print(l)
# data_delete = int(input())
# print(l.remove(data_delete))
main()