Skip to content

Commit c86829c

Browse files
committed
Update1: Linkedlist and Stack
0 parents  commit c86829c

8 files changed

+786
-0
lines changed

Linked_list_functions.py

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
class Node:
2+
def __init__(self, data=None, next=None):
3+
self.data=data
4+
self.next=next
5+
6+
class LinkedList:
7+
def __init__(self):
8+
self.head=None
9+
10+
def check_empty(self):
11+
if self.head is None:
12+
return True
13+
return False
14+
15+
def insert(self, data):
16+
if self.head is None:
17+
self.head=Node(data, None)
18+
return
19+
itr=self.head
20+
while itr.next:
21+
itr=itr.next
22+
itr.next=Node(data, None)
23+
24+
def search_by_data(self, data):
25+
'''here, if data is found then we'll return it's index position'''
26+
if not self.head:
27+
print("LL is empty, can't perform search")
28+
return
29+
position=0
30+
itr=self.head
31+
while itr:
32+
if itr.data==data:
33+
print(f"data present at index:{position}")
34+
break
35+
position+=1
36+
itr=itr.next
37+
else:
38+
print("data not present")
39+
40+
41+
def find_ref_to_node(self, data):
42+
'''here we'll find the next reference of given data'''
43+
if self.head is None:
44+
print("LL is empty")
45+
return
46+
itr=self.head
47+
while itr:
48+
if itr.data==data:
49+
if itr.next is None:
50+
print("Current node is pointing to None")
51+
break
52+
print("Current node is pointing to:", str(itr.next.data))
53+
return
54+
itr=itr.next
55+
56+
57+
def insert_after_given_node(self,node_data,insert_data ):
58+
try:
59+
assert self.head is not None
60+
except AssertionError:
61+
print("LL is empty")
62+
else:
63+
itr=self.head
64+
while itr:
65+
if itr.data == node_data:
66+
node=Node(insert_data, itr.next)
67+
itr.next=node
68+
break
69+
itr=itr.next
70+
def delete_at_beginning(self):
71+
if self.head is None:
72+
print("LL is empty")
73+
return
74+
self.head=self.head.next
75+
76+
def deletion_last_node(self):
77+
if self.head is None:
78+
print("LL is empty")
79+
return
80+
81+
itr=self.head
82+
while itr:
83+
if itr.next.next is None:
84+
itr.next=None
85+
itr=itr.next
86+
87+
def delete_node(self, data):
88+
if self.head is None:
89+
print("LL is empty")
90+
return
91+
itr=self.head
92+
93+
if self.head.data==data:
94+
self.head=self.head.next
95+
return
96+
97+
while itr:
98+
if itr.next is None:
99+
print("value not available in the LinkedList")
100+
break
101+
if itr.next.data==data:
102+
itr.next=itr.next.next
103+
break
104+
itr=itr.next
105+
else:
106+
print("data not founds")
107+
108+
def find_miidle(self):
109+
if self.head is None:
110+
print("LL is empty")
111+
return
112+
slow_ptr=self.head
113+
fast_ptr=self.head
114+
115+
while fast_ptr and fast_ptr.next:
116+
# if fast_ptr.next.next is None:
117+
# print("middle of LL is", str(slow_ptr.data))
118+
# break
119+
slow_ptr=slow_ptr.next
120+
fast_ptr=fast_ptr.next.next
121+
print("middle of LL is", str(slow_ptr.data))
122+
123+
def get_len(self):
124+
if self.head is None:
125+
return 0
126+
itr=self.head
127+
count=0
128+
while itr:
129+
count+=1
130+
itr=itr.next
131+
return count
132+
133+
def reverse_LL(self):
134+
current=self.head
135+
prev=None
136+
while current !=None:
137+
temp=current.next
138+
current.next=prev
139+
prev=current
140+
current=temp
141+
self.head=prev
142+
143+
144+
def display_LL(self):
145+
if self.head is None:
146+
print("LL is empty")
147+
return
148+
itr=self.head
149+
while itr:
150+
print(itr.data,end="-->")
151+
# print("%d"%(itr.data),end="-->")
152+
itr=itr.next
153+
print()
154+
'''
155+
LL=LinkedList()
156+
LL.insert(10)
157+
LL.insert(20)
158+
LL.insert(30)
159+
LL.insert(40)
160+
LL.insert(50)
161+
LL.insert(60)
162+
LL.display_LL()
163+
LL.search_by_data(30)
164+
LL.find_ref_to_node(20)
165+
# print(LL.find_ref_to_node.__doc__)
166+
LL.insert_after_given_node(10, 15)
167+
LL.display_LL()
168+
LL.delete_at_beginning()
169+
LL.display_LL()
170+
LL.deletion_last_node()
171+
LL.display_LL()
172+
LL.delete_node(20)
173+
LL.display_LL()
174+
LL.find_miidle()
175+
print(LL.get_len())'''
176+
177+
LL=LinkedList()
178+
LL.insert(10)
179+
LL.insert(20)
180+
LL.insert(30)
181+
LL.insert(40)
182+
LL.reverse_LL()
183+
LL.display_LL()
184+
185+
# LL.insert(10)
186+
# LL.insert(20)
187+
# LL.insert(30)
188+
# LL.insert(40)
189+
# LL.find_miidle()

doubly_linked_list.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# for garbege collection
2+
import gc
3+
4+
class Node:
5+
def __init__(self, data= None, prev= None, next= None,):
6+
self.data=data
7+
self.next=next
8+
self.prev=prev
9+
10+
class Doubly_linked_list:
11+
def __init__(self):
12+
self.head=None
13+
14+
15+
def insert_at_beginning(self, data):
16+
# check if DLL is empty
17+
if self.head is None:
18+
# if empty insert at beginning
19+
node=Node(data, None, None)
20+
# now head will point to newly created node
21+
self.head=node
22+
return
23+
# if not empty create a node pointing to current head
24+
node=Node(data,None, self.head )
25+
# now hwad will be marked as newly created node bcz we're inserting at beginning
26+
self.head=node
27+
28+
def delete_beginning(self):
29+
if self.head is None:
30+
print("DLL is Empty")
31+
return
32+
self.head=self.head.next
33+
self.head.prev=None
34+
35+
def delete_end(self):
36+
if self.head is None:
37+
print("DLL is empty")
38+
return
39+
itr=self.head
40+
while itr.next.next:
41+
itr=itr.next
42+
itr.next=None
43+
44+
def insert_at_end(self, data):
45+
# check if DLL is empty
46+
if self.head is None:
47+
# if empty insert at beginning
48+
self.insert_at_beginning(data)
49+
return
50+
# if not empty, travese till end
51+
# head will be assigned to iterator
52+
itr=self.head
53+
# it will iterate untill itr.next points to None i.e. it has reached end
54+
while itr.next:
55+
# each step, next node will be assigned to iterator
56+
itr=itr.next
57+
58+
# we've reached att end, so we'll create a new node
59+
# whose prev will point to current last node(itr) and end will point to None
60+
node=Node(data, itr, None)
61+
itr.next=node
62+
63+
def get_len(self):
64+
if self.head is None:
65+
return 0
66+
count=0
67+
itr=self.head
68+
while itr:
69+
count+=1
70+
itr=itr.next
71+
return count
72+
73+
def delete_node_by_val(self, data):
74+
if self.head is None:
75+
print("DLL is empty")
76+
return
77+
# if data present at beginning
78+
if self.head.data==data:
79+
self.delete_beginning()
80+
return
81+
82+
itr=self.head
83+
# if node is in between start and end
84+
while itr.next.next:
85+
if itr.next.data==data:
86+
temp=itr.next.next
87+
itr.next=itr.next.next
88+
temp.prev=itr
89+
return
90+
itr=itr.next
91+
92+
# if node present at the end
93+
if itr.next.data==data:
94+
self.delete_end()
95+
else:
96+
print(f"node containing data {data} is not present in DLL")
97+
gc.collect()
98+
99+
def insert_after_node(self, node_data, insert_data):
100+
if self.head is None:
101+
print("DLL is empty")
102+
return
103+
itr=self.head
104+
while itr.next:
105+
if itr.data==node_data:
106+
temp=itr.next
107+
node=Node(insert_data, itr,itr.next)
108+
itr.next=node
109+
temp.prev=node
110+
break
111+
itr=itr.next
112+
if itr.data==node_data:
113+
self.insert_at_end(insert_data)
114+
else:
115+
print(f"node data {node_data} not present in DLL")
116+
117+
118+
def print_doubly_LL(self):
119+
# check if empty
120+
if self.head is None:
121+
print("Doubly LL is empty")
122+
return
123+
# if not empty, iterate and print
124+
itr=self.head
125+
while itr:
126+
print(str(itr.data),end="-->")
127+
itr=itr.next
128+
print()
129+
130+
def print_reverse_DLL(self):
131+
if self.head is None:
132+
print("DLL is empty")
133+
return
134+
itr=self.head
135+
# here we'll get the last node
136+
while itr.next:
137+
itr=itr.next
138+
# we have it as our last node
139+
while itr:
140+
print(str(itr.data),end="-->")
141+
itr=itr.prev
142+
143+
'''DLL=Doubly_linked_list()
144+
DLL.insert_at_beginning(10)
145+
DLL.insert_at_beginning(20)
146+
DLL.insert_at_beginning(30)
147+
DLL.insert_at_beginning(40)
148+
DLL.print_doubly_LL()'''
149+
150+
DLL=Doubly_linked_list()
151+
DLL.insert_at_end(10)
152+
DLL.insert_at_end(20)
153+
DLL.insert_at_end(30)
154+
DLL.insert_at_end(40)
155+
DLL.insert_at_end(50)
156+
DLL.print_doubly_LL()
157+
DLL.insert_after_node(50, 60)
158+
DLL.print_doubly_LL()
159+
# DLL.delete_node_by_val(10)
160+
# DLL.print_doubly_LL()
161+
# DLL.print_doubly_LL()
162+
# DLL.delete_beginning()
163+
# DLL.delete_end()
164+
# DLL.print_doubly_LL()
165+
# print(DLL.get_len())
166+
DLL.print_reverse_DLL()

0 commit comments

Comments
 (0)