-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubleLinkedList.py
More file actions
150 lines (134 loc) · 3.81 KB
/
doubleLinkedList.py
File metadata and controls
150 lines (134 loc) · 3.81 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class Node:
def __init__(self,item):
self.__data=item
self.__next=None
self.__previous=None
@property
def getData(self):
return self.__data
def setData(self,item):
self.__data=item
@property
def getNext(self):
return self.__next
def setNext(self,newNext):
self.__next=newNext
@property
def getPrevious(self):
return self.__previous
def setPrevious(self,newPrev):
self.__previous=newPrev
class DlinkedList:
def __init__(self):
self.__head=None
self.__last=None
def __str__(self):
current=self.__head
list=[]
while current != None:
list.append(current.getData)
current=current.getNext
return str(list)
def __iter__(self):
current=self.__head
list=[]
while current != None:
list.append(current.getData)
current=current.getNext
return iter(list)
def __len__(self):
current=self.__head
count=0
while current != None:
current=current.getNext
count+=1
return count
@property
def getFirst(self):
return self.__head.getData
@property
def getLast(self):
return self.__last.getData
@property
def isEmpty(self):
if self.__last ==None and self.__head==None:
return True
return False
def insertAtBeggin(self,data):
item=Node(data)
if self.isEmpty:
self.__head=item
self.__last=item
return
current=self.__head
item.setNext(current)
current.setPrevious(item)
self.__head=item
def insertAtEnd(self,data):
item=Node(data)
if self.isEmpty:
self.__head=item
self.__last=item
return
current=self.__last
item.setPrevious(current)
current.setNext(item)
self.__last=item
def insert(self,data,pos:int):
if pos<0 or pos > len(self):
return "Invalid position"
elif pos==0:
self.insertAtBeggin(data)
return
elif pos == len(self):
self.insertAtEnd(data)
return
item=Node(data)
current=self.__head
count=0
while count < pos-1:
current=current.getNext
count+=1
item.setNext(current.getNext)
item.setPrevious(current)
temp:Node=current.getNext
temp.setPrevious(item)
current.setNext(item)
def deleteLast(self):
if self.isEmpty:
return "The list is empty!"
elif len(self)==1 and self.__head==self.__last:
self.__head=None
self.__last=None
return
current=self.__last
prev:Node=current.getPrevious
prev.setNext(None)
self.__last=prev
def deleteFirst(self):
if self.isEmpty:
return"The list is empty!"
elif len(self)==1 and self.__head ==self.__last:
self.__head=None
self.__last=None
return
current=self.__head
next:Node=current.getNext
next.setPrevious(None)
self.__head=next
def deleteData(self,data):
if self.isEmpty:
return "The list is empty!"
current=self.__head
while current.getData !=data:
current=current.getNext
if current==self.__head:
self.deleteFirst()
return
elif current==self.__last:
self.deleteLast()
return
next:Node=current.getNext
prev:Node=current.getPrevious
next.setPrevious(prev)
prev.setNext(next)