-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doubly LinkedList.py
178 lines (147 loc) · 4.37 KB
/
Doubly LinkedList.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
class Node:
def __init__(self,data):
self.data = data
self.nref = None
self.pref = None
class Doubly_LinkedList:
def __init__(self):
self.head = None
def traverse_forward(self):
if self.head is None:
print("Linked List is empty")
else:
n = self.head
while n is not None:
print(n.data, end="==>")
n = n.nref
def traverse_backward(self):
print()
if self.head is None:
print("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, end="<==")
n = n.pref
def insert_empty(self, data):
if self.head is None:
new_node = Node(data)
self.head = new_node
else:
print("LinkedList not empty")
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_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_after(self, data, x):
if self.head is None:
print("LinkedList is empty")
else:
n = self.head
while n is not None:
if x == n.data:
break
n = n.nref
if n is None:
print("Node is not found")
new_node = Node(data)
new_node.nref = n.nref
new_node.pref = n
if n.nref is not None:
n.nref.pref = new_node
n.nref = new_node
def add_before(self, data, x):
if self.head is None:
print("LinkedList is empty")
else:
n = self.head
while n is not None:
if x == n.data:
break
n = n.nref
if n is None:
print("Node is not found")
else:
new_node = Node(data)
new_node.nref = n
new_node.pref = n.pref
if n.pref is not None:
n.pref.nref = new_node
else:
self.head = new_node
n.pref = new_node
def delete_begin(self):
if self.head is None:
print("LinkedList is empty")
elif self.head.nref is None:
self.head = None
else:
self.head = self.head.nref
self.head.pref = None
def delete_end(self):
if self.head is None:
print("LinkedList is empty")
elif self.head.nref is None:
self.head = None
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("LinkedList is empty")
return
if self.head.nref is None:
if self.head == x:
self.head = None
else:
print("Data is not found in Linkedlist")
return
if self.head.data == x:
self.head = self.head.nref
self.head.pref = None
return
n = self.head
while n.nref is not None:
if x == n.data:
break
n = n.nref
if n.nref is not None:
n.pref.nref = n.nref
n.nref.pref = n.pref
else:
if n.data == x:
n.pref.nref = None
else:
print("Data is not found in Linkedlist")
ll = Doubly_LinkedList()
ll.insert_empty(20)
ll.add_end(100)
ll.add_begin(10)
ll.add_after(30,20)
ll.add_after(110,100)
ll.add_before(15,20)
ll.add_before(5, 10)
ll.delete_begin()
ll.delete_end()
ll.delete_by_value(15)
ll.delete_by_value(100)
ll.traverse_forward()
ll.traverse_backward()