forked from hariom20singh/python-learning-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoublyLinkedList.py
More file actions
122 lines (110 loc) · 3.37 KB
/
doublyLinkedList.py
File metadata and controls
122 lines (110 loc) · 3.37 KB
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
class Node:
def __init__(self, data):
self.item = data
self.next = None
self.prev = None
# Class for doubly Linked List
class doublyLinkedList:
def __init__(self):
self.start_node = None
# Insert Element to Empty list
def InsertToEmptyList(self, data):
if self.start_node is None:
new_node = Node(data)
self.start_node = new_node
else:
print("The list is empty")
# Insert element at the end
def InsertToEnd(self, data):
# Check if the list is empty
if self.start_node is None:
new_node = Node(data)
self.start_node = new_node
return
n = self.start_node
# Iterate till the next reaches NULL
while n.next is not None:
n = n.next
new_node = Node(data)
n.next = new_node
new_node.prev = n
# Delete the elements from the start
def DeleteAtStart(self):
if self.start_node is None:
print("The Linked list is empty, no element to delete")
return
if self.start_node.next is None:
self.start_node = None
return
self.start_node = self.start_node.next
self.start_prev = None;
# Delete the elements from the end
def delete_at_end(self):
# Check if the List is empty
if self.start_node is None:
print("The Linked list is empty, no element to delete")
return
if self.start_node.next is None:
self.start_node = None
return
n = self.start_node
while n.next is not None:
n = n.next
n.prev.next = None
def counting_nodes(self):
n = self.start_node
t=0
while n is not None:
t=t+1
n=n.next
print(f"No of nodes is {t}")
# Traversing and Displaying each element of the list
def duplicate(self):
lis2=[]
n = self.start_node
while n is not None:
if n.item in lis2:
n.prev.next=n.next
else:
lis2.append(n.item)
n=n.next
# for i in range(len(lis2)):
# print(f"Element is: {lis2[i]}")
self.Display()
def Display(self):
if self.start_node is None:
print("The list is empty")
return
else:
n = self.start_node
while n is not None:
print("Element is: ", n.item)
n = n.next
print("\n")
# Create a new Doubly Linked List
NewDoublyLinkedList = doublyLinkedList()
# Insert the element to empty list
n=int(input("Enter the no nodes\n"))
lis=[]
print("Enter the element\n")
for i in range(n):
a=int(input())
lis.append(a)
NewDoublyLinkedList.InsertToEmptyList(lis[0])
# Insert the element at the end
for i in range(1,n):
NewDoublyLinkedList.InsertToEnd(lis[i])
NewDoublyLinkedList.Display()
# Counting the nodes
NewDoublyLinkedList.counting_nodes()
# Deleting last two elements
NewDoublyLinkedList.delete_at_end()
NewDoublyLinkedList.delete_at_end()
print("After deleting element\n")
# Counting the nodes
NewDoublyLinkedList.counting_nodes()
# Display Data
NewDoublyLinkedList.Display()
# removing duplicate
print("\nRemoving duplicate\n")
NewDoublyLinkedList.duplicate()