-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked_list_functions.py
189 lines (168 loc) · 4.92 KB
/
Linked_list_functions.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
177
178
179
180
181
182
183
184
185
186
187
188
189
class Node:
def __init__(self, data=None, next=None):
self.data=data
self.next=next
class LinkedList:
def __init__(self):
self.head=None
def check_empty(self):
if self.head is None:
return True
return False
def insert(self, data):
if self.head is None:
self.head=Node(data, None)
return
itr=self.head
while itr.next:
itr=itr.next
itr.next=Node(data, None)
def search_by_data(self, data):
'''here, if data is found then we'll return it's index position'''
if not self.head:
print("LL is empty, can't perform search")
return
position=0
itr=self.head
while itr:
if itr.data==data:
print(f"data present at index:{position}")
break
position+=1
itr=itr.next
else:
print("data not present")
def find_ref_to_node(self, data):
'''here we'll find the next reference of given data'''
if self.head is None:
print("LL is empty")
return
itr=self.head
while itr:
if itr.data==data:
if itr.next is None:
print("Current node is pointing to None")
break
print("Current node is pointing to:", str(itr.next.data))
return
itr=itr.next
def insert_after_given_node(self,node_data,insert_data ):
try:
assert self.head is not None
except AssertionError:
print("LL is empty")
else:
itr=self.head
while itr:
if itr.data == node_data:
node=Node(insert_data, itr.next)
itr.next=node
break
itr=itr.next
def delete_at_beginning(self):
if self.head is None:
print("LL is empty")
return
self.head=self.head.next
def deletion_last_node(self):
if self.head is None:
print("LL is empty")
return
itr=self.head
while itr:
if itr.next.next is None:
itr.next=None
itr=itr.next
def delete_node(self, data):
if self.head is None:
print("LL is empty")
return
itr=self.head
if self.head.data==data:
self.head=self.head.next
return
while itr:
if itr.next is None:
print("value not available in the LinkedList")
break
if itr.next.data==data:
itr.next=itr.next.next
break
itr=itr.next
else:
print("data not founds")
def find_miidle(self):
if self.head is None:
print("LL is empty")
return
slow_ptr=self.head
fast_ptr=self.head
while fast_ptr and fast_ptr.next:
# if fast_ptr.next.next is None:
# print("middle of LL is", str(slow_ptr.data))
# break
slow_ptr=slow_ptr.next
fast_ptr=fast_ptr.next.next
print("middle of LL is", str(slow_ptr.data))
def get_len(self):
if self.head is None:
return 0
itr=self.head
count=0
while itr:
count+=1
itr=itr.next
return count
def reverse_LL(self):
current=self.head
prev=None
while current !=None:
temp=current.next
current.next=prev
prev=current
current=temp
self.head=prev
def display_LL(self):
if self.head is None:
print("LL is empty")
return
itr=self.head
while itr:
print(itr.data,end="-->")
# print("%d"%(itr.data),end="-->")
itr=itr.next
print()
'''
LL=LinkedList()
LL.insert(10)
LL.insert(20)
LL.insert(30)
LL.insert(40)
LL.insert(50)
LL.insert(60)
LL.display_LL()
LL.search_by_data(30)
LL.find_ref_to_node(20)
# print(LL.find_ref_to_node.__doc__)
LL.insert_after_given_node(10, 15)
LL.display_LL()
LL.delete_at_beginning()
LL.display_LL()
LL.deletion_last_node()
LL.display_LL()
LL.delete_node(20)
LL.display_LL()
LL.find_miidle()
print(LL.get_len())'''
LL=LinkedList()
LL.insert(10)
LL.insert(20)
LL.insert(30)
LL.insert(40)
LL.reverse_LL()
LL.display_LL()
# LL.insert(10)
# LL.insert(20)
# LL.insert(30)
# LL.insert(40)
# LL.find_miidle()