-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDoubly_Circular_linked_list.py
114 lines (100 loc) · 3.4 KB
/
Doubly_Circular_linked_list.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class DoublyCLL:
def __init__(self):
self.last = None
def insertEmpty(self, data):
if self.last is None:
newNode = Node(data)
newNode.next = newNode
newNode.prev = None
self.last = newNode
return self.last
else:
return "List is not empty"
def insertBeg(self, data):
if self.last is None:
print("List is empty, Insertion Will Happen Thou")
self.insertEmpty(data)
else:
newNode = Node(data)
newNode.next = self.last
newNode.prev = self.last
self.last.prev = newNode
self.last.next = newNode
return self.last
def insertEnd(self, data):
newNode = Node(data)
if self.last is None:
print("List is empty, Insertion Will Happen Thou")
self.insertEmpty(data)
else:
newNode.next = self.last.next
newNode.prev = self.last
self.last.next = newNode
self.last = newNode
return self.last
def insertBetween(self, data, position):
if self.last is None:
print("List is empty, Insertion Will Happen Thou")
self.insertEmpty(data)
else:
newNode = Node(data)
temp = self.last.next
while temp.next != self.last.next:
if temp.data == position:
newNode.next = temp.next
newNode.prev = temp
temp.next.prev = newNode
temp.next = newNode
return self.last
if temp.next == self.last:
self.last = new_node
return self.last
temp = temp.next
if temp.next == self.last.next:
print("Position not found")
def traverse(self):
if self.last is None:
print("List is empty")
return
else:
temp = self.last.next
while temp:
print(temp.data, end=" ")
temp = temp.next
if temp == self.last.next:
break
def delete(self, data):
if self.last is None:
print("List is empty")
return
else:
temp = self.last.next
while temp:
if temp.data == data:
temp.prev.next = temp.next
temp.next.prev = temp.prev
return self.last
temp = temp.next
def current(self):
if self.last is None:
print("List is empty")
return
else:
return self.last.prev.prev.data
DCLL = DoublyCLL()
print("Insertion Of: ", DCLL.insertEmpty(1).data, " at empty CLL is Successful")
print("Insertion Of: ", DCLL.insertBeg(2).next.data, " at beg CLL is Successful")
print("Insertion Of: ", DCLL.insertEnd(3).data, " at end CLL is Successful")
print("Insertion Of: ", DCLL.insertBetween(4, 2).next.next.data, " at position 2 is Successful")
print("Traversal of DCLL is Successful")
DCLL.traverse()
print("\n")
print("Deletion of: ", DCLL.current(), " is Successful")
DCLL.delete(DCLL.current())
print("Traversal of DCLL is Successful")
DCLL.traverse()