-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoublyLinkedList.py
79 lines (68 loc) · 2.31 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
class DoublyLinkedList:
def __init__(self,value):
self.head={'value':value, 'next':None, 'prev':None}
self.tail=self.head
self.length=1
def printList(self):
myList=[]
currentNode=self.head
while currentNode != None:
myList.append(currentNode['value'])
currentNode=currentNode['next']
return myList
def traverse(self,index):
currentNode=self.head
count=0
while count!=index-1:
currentNode=currentNode['next']
count+=1
return currentNode
def append(self,value):
newNode={'value':value,'next':None, 'prev':None}
newNode['prev']=self.tail
self.tail['next']=newNode
self.tail=newNode
self.length+=1
return self.head
def prepend(self,value):
newNode={'value':value,'next':None,'prev':None}
self.head['prev']=newNode
newNode['next']=self.head
self.head=newNode
self.length+=1
return self.head
def insert(self,index,value):
if index > self.length:
return self.append(value)
else:
newNode={'value':value,'next':None,'prev':None}
currentNode=self.traverse(index)
newNode['prev']=currentNode
nextNode=currentNode['next']
nextNode['prev']=newNode
currentNode['next']=newNode
newNode['next']=nextNode
self.length+=1
return self.head
def delete(self,index):
currentNode=self.traverse(index)
toBeRemovedNode=currentNode['next']
nextNode=toBeRemovedNode['next']
currentNode['next']=nextNode
nextNode['prev']=currentNode
self.length-=1
return self.head
def reverse(self):
if self.length == 1:
return self.head
else:
currentNode=self.tail
self.head=self.tail
while currentNode != None:
temp=currentNode['next']
currentNode['next']=currentNode['prev']
currentNode['prev']=temp
currentNode=currentNode['next']
self.tail=currentNode
self.head['prev']=None
return self.head