-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData_structre_Doubly_linkedlist.py
62 lines (61 loc) · 1.53 KB
/
Data_structre_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
class Node:
def __init__(self,data):
self.data=data
self.next=None
self.prev=None
class DoublyLinkedList:
def __init__(self):
self.head=None
self.trail=None
self.size=0
def add(self, data):
if self.head == None:
new_node=Node(data)
self.head=new_node
self.size=self.size+1
return
new_node=Node(data)
is_next=self.head
while is_next.next != None:
is_next = is_next.next
is_next.next = new_node
new_node.prev=is_next
self.trail = new_node
def show(self):
if self.head is None:
return
is_prev=self.trail
while is_prev:
print(is_prev.data)
is_prev = is_prev.prev
def show2(self):
if self.head is None:
return
is_next=self.head
while is_next:
print(is_next.data)
is_next=is_next.next
def remove(self,data):
if self.head.data == data:
self.head = self.head.next
self.head.prev = None
return
is_next= self.head
back=None
while is_next:
if is_next.data == data:
back.next = is_next.next
is_next.next.prev = back
return
back = is_next
is_next = is_next.next
dl=DoublyLinkedList()
dl.add("tata motors")
dl.add("kia motors")
dl.add("Nissan")
dl.show()
print()
dl.show2()
print()
dl.remove("kia motors")
dl.show2()