-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubly_linked_list.py
71 lines (69 loc) · 1.99 KB
/
doubly_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
class Node:
def __init__(self, value=None,nextNode=None,previousNode=None):
self.value=value
self.nextNode=nextNode
self.previousNode=previousNode
class DoublyLinkedList:
def __init__(self,node=None):
self.head=node
self.size=0
def print_list(self):
head =self.head
while head:
print(f"The value is {head.value}")
head=head.nextNode
def addNodeTop(self,value):
newNode=Node(value)
newNode.nextNode=self.head
head=self.head
if head :
head.previousNode=newNode
self.head=newNode
self.size+=1
def addNodeBottom(self,value):
newNode=Node(value)
if self.head==None:
self.addNodeTop(value)
return
head=self.head
while head:
previous=head
head=head.nextNode
previous.nextNode=newNode
newNode.previousNode=previous
self.size+=1
def delete(self,value):
head=self.head
previous=None
while head:
if head.value==value:
if previous is None:
self.head=head.nextNode
self.head.previousNode=None
else:
previous.nextNode=head.nextNode
head.nextNode=previous
return
else:
previous=head
head=head.nextNode
def search(self,val):
head=self.head
while head:
if head.value==val:
return head
else:
head=head.nextNode
def replaceNodeValue(self,newVal,val):
node=self.search(val)
node.value=newVal
def reverseList(self):
head=self.head
previous=None
while head:
temp_head=head
head=head.nextNode
temp_head.nextNode=previous
temp_head.previousNode=head
previous=temp_head
self.head=previous