-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.py
100 lines (89 loc) · 2.58 KB
/
DoublyLinkedList.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
class Node:
def __init__(self, data):
self.item = data
self.next = None
self.prev = None
class doublyLinkedList:
def __init__(self):
self.head = None
def InsertionEmpList(self, data):
if self.head is None:
new_node = Node(data)
self.head = new_node
else:
print("The list is empty")
def InsertionEnd(self, data):
if self.head is None:
new_node = Node(data)
self.head = new_node
return
n = self.head
while n.next is not None:
n = n.next
new_node = Node(data)
n.next = new_node
new_node.prev = n
def DeletionStart(self):
if self.head is None:
print("The Linked list is empty, no element to delete")
return
if self.head.next is None:
self.head = None
return
self.head = self.head.next
self.start_prev = None
def DeletionEnd(self):
if self.head is None:
print("The Linked list is empty, no element to delete")
return
if self.head.next is None:
self.head = None
return
n = self.head
while n.next is not None:
n = n.next
n.prev.next = None
def PrintList(self):
if self.head is None:
print("The list is empty")
return
else:
n = self.head
while n is not None:
print("Element is: ", n.item)
n = n.next
print("\n")
def Search(self, target):
i = 1
Value = False
curr = self.head
if(self.head == None):
print("List is empty")
return
while(curr != None):
if(curr.item == target):
Value = True
break
curr = curr.next
i = i + 1
if(Value):
print("The node is present in the list at position : ")
print(i)
else:
print("The node isn't present in the list")
# Driver Code
DoublyLList = doublyLinkedList()
print("Insertion :")
DoublyLList.InsertionEmpList(10)
DoublyLList.InsertionEnd(20)
DoublyLList.InsertionEnd(30)
DoublyLList.InsertionEnd(40)
DoublyLList.InsertionEnd(50)
DoublyLList.InsertionEnd(60)
DoublyLList.PrintList()
print("Deletion :")
DoublyLList.DeletionStart()
DoublyLList.DeletionStart()
DoublyLList.PrintList()
print("Searching :")
DoublyLList.Search(40)