forked from SamirPaulb/Problem-Solving
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Total Double Linked List in one.py
175 lines (155 loc) · 4.48 KB
/
Total Double Linked List in one.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
# ***** DOUBLY LINKED LIST IN PYTHON *****
# Print from forward direction
# Print from backward derection
# Add end
# add begin
# Add after a node
# add before a node
# Delete at beginning
# Delete at the end
# Delete element at a given value
class Node:
def __init__(self,data):
self.data = data
self.nref = None
self.pref = None
class DoublyLL:
def __init__(self):
self.head = None
def print_ll(self):
if self.head is None:
print("The Doubly linked list is empty")
else:
n = self.head
while n is not None:
print(n.data)
n = n.nref
def print_ll_reverse(self):
if self.head is None:
print("The Doubly linked list is empty")
else:
n = self.head
while n.nref is not None:
n = n.nref
while n is not None:
print(n.data)
n = n.pref
def add_end(self,data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
n = self.head
while n.nref is not None:
n = n.nref
n.nref = new_node
new_node.pref = n
def add_begin(self,data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
new_node.nref = self.head
self.head.pref = new_node
self.head = new_node
def add_after_node(self,data,x):
new_node = Node(data)
n = self.head
while n is not None:
if n.data ==x:
break
n = n.nref
if n is None:
print("We can't add after None Value, out of bound")
else:
if n.nref is None:
n.nref = new_node
new_node.pref = n
else:
new_node.nref = n.nref
n.nref.pref = new_node
n.nref = new_node
new_node.pref = n
def add_before_node(self,data,x):
if self.head is None:
print("Linked List is Empty!")
return
if self.head.data == x:
new_node = Node(data)
self.head.pref = new_node
new_node.nref = self.head
self.head = new_node
return
n = self.head
while n.nref is not None:
if x == n.nref.data:
break
n = n.nref
if n.nref is None:
print("Given Node is not presnt in Linked List!")
else:
new_node = Node(data)
new_node.pref = n
new_node.nref = n.nref
n.nref.pref = new_node
n.nref = new_node
def delete_begine(self):
if self.head is None:
print("DLL is empty so can't delete")
return
if self.head.nref == None:
self.head = None
return
else:
self.head = self.head.nref
self.head.pref = None
def delete_end(self):
if self.head is None:
print("DLL is empty so can't delete")
return
else:
n = self.head
while n.nref is not None:
n = n.nref
n.pref.nref = None
def delete_by_value(self,x):
if self.head is None:
print("DLL is empty so can't delete")
return
if self.head.nref is None:
if self.head.data == x:
self.head = None
else:
print("Given value is not present")
return
if self.head.data == x:
self.head = self.head.nref
self.head.pref = None
return
else:
n = self.head
while n.nref is not None:
if n.data == x:
break
n = n.nref
if n.nref is not None:
n.pref.nref = n.nref
n.nref = None
n.pref = None
else:
if n.data ==x:
n.pref.nref = None
else:
print(f"Can't delete {x} as it is not present")
DLL = DoublyLL()
DLL.add_begin(20)
DLL.add_begin(10)
DLL.add_end(50)
DLL.add_after_node(30,20)
DLL.add_before_node(40,50)
DLL.delete_begine()
DLL.delete_end()
DLL.delete_by_value(100)
DLL.print_ll()
print("----------------------------")
DLL.print_ll_reverse()