-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdll_operations.py
57 lines (43 loc) · 1.38 KB
/
dll_operations.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
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
self.prev = None
class DoubleLL:
def __init__(self):
self.head = None
def traverse_ll(self):
if self.head is None:
print("LL has no element")
return
else:
iterator = self.head
while iterator is not None:
print(str(iterator.data) , end="-->")
iterator = iterator.next
def insert_ll(self, data):
node=Node(data)
node.next=self.head
if self.head is not None:
self.head.prev = node
self.head = node
def delete_ll(self, delete_node):
if self.head is None or delete_node is None:
return
if self.head == delete_node:
self.head=delete_node.next
if delete_node.next is not None:
delete_node.next.prev = delete_node.prev
if delete_node.prev is not None:
delete_node.prev.next = delete_node.next
dll_list1 = DoubleLL()
dll_list1.insert_ll(2)
dll_list1.insert_ll(4)
dll_list1.insert_ll(5)
dll_list1.insert_ll(10)
dll_list1.insert_ll(12)
print("Doubly Linked List After insertion of nodes")
dll_list1.traverse_ll()
dll_list1.delete_ll(dll_list1.head)
print("\n Doubly Linked List after deletion")
dll_list1.traverse_ll()