-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubly_linked_list.py
More file actions
222 lines (195 loc) · 6.48 KB
/
doubly_linked_list.py
File metadata and controls
222 lines (195 loc) · 6.48 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# Create a Doubly Linked list
from doubly_linked_node import DoublyLinkedNode
class DoublyLinkedList:
def __init__(self):
self.head = None
self.count = 0
self.counter_val = None
self.count = 0
def __iter__(self):
if self.head:
self.counter_val = self.head
return self
return "Empty List"
def __next__(self):
if self.counter_val == None:
raise StopIteration
if self.counter_val.next != None:
self.counter_val = self.counter_val.next
return self.counter_val.prev
else:
temp_val = self.counter_val
self.counter_val = self.counter_val.next
return temp_val
def get(self, val):
if self.head == None:
return -1
else:
count = 0
temp_val = self.head
while temp_val.next != None:
if temp_val.value == val:
return count
else:
temp_val = temp_val.next
count += 1
return -1
def get_at_index(self, index):
# print("11111" + str(index))
if self.head == None:
return IndexError("No values exist")
elif index == 0:
return self.head
else:
count = 0
temp_val = self.head
while count != index:
if temp_val.next == None:
raise IndexError(str.format("Index {} doesn't exist", index))
temp_val = temp_val.next
count += 1
if count != index:
raise IndexError(str.format("Index {} doesn't exist", index))
return temp_val
def remove_duplicates(self, val):
if self.head == None:
return False
count = 0
has_appeared = False
temp_val = self.head
while temp_val != None or temp_val.next != None:
if temp_val.value == val and has_appeared:
pass
# TODO
def add(self, value):
if self.head == None:
self.head = DoublyLinkedNode(value)
else:
temp_val = self.head
while temp_val.next != None:
temp_val = temp_val.next
temp_val.next = DoublyLinkedNode(value)
temp_val.next.prev = temp_val
self.count += 1
def add_at_front(self, value):
if self.head == None:
self.head = DoublyLinkedNode(value)
else:
temp_val = DoublyLinkedNode(value)
temp_val.next = self.head
self.head = temp_val
self.count += 1
def remove_end(self):
if self.head == None:
raise IndexError("Empty List")
if self.count == 1:
self.head = None
self.count -= 1
return
temp_val = self.head
for x in range(1, self.count):
temp_val = temp_val.next
temp_val.remove()
self.count -= 1
def remove_at_index(self, index):
if index >= self.count:
return False
if index == 0 and self.count > 1:
temp_val = self.head
self.head = self.head.next
temp_val.remove()
self.count -= 1
return True
elif index == 0:
temp_val = self.head
self.head = None
temp_val.remove()
self.count -= 1
return True
temp_val = self.head
for x in range(1, index):
temp_val = temp_val.next
temp_val.remove()
self.count -= 1
return True
def remove(self, value, remove_all=False):
if self.head == None:
return False
elif self.head.value == value and self.head.next == None:
self.head = None
self.count -= 1
return True
elif self.head.value == value and self.head.next != None:
self.head.next.prev = None
self.head = self.head.next
self.count -= 1
return True
else:
temp_val = self.head
while temp_val.next != None:
#print(str.format("self.tv.value = {}",temp_val.value))
#print(str.format("self.tvn.value = {}",temp_val.next.value))
if temp_val.next.value == value and temp_val.next.next != None:
#print(str.format("Entered here"))
next = temp_val.next.next
temp_val.next.next.prev = temp_val
temp_val.next.prev = None
temp_val.next.next = None
temp_val.next = next
if remove_all == False:
break
elif temp_val.next.value == value and temp_val.next.next == None:
#print(str.format("Entered there"))
temp_val.next.prev = None
temp_val.next = None
if remove_all == False:
break
else:
temp_val = temp_val.next
self.count -= 1
def print_list(self, return_string=False):
val = ""
if self.head == None:
val += "Empty List"
return val
temp_val = self.head
while temp_val.next != None and temp_val.value != None:
val += str(temp_val.value)
val += "\n"
temp_val = temp_val.next
val += str(temp_val.value)
if return_string == True:
return val
print(val)
if __name__ == "__main__":
var = DoublyLinkedList()
var.add(1)
var.add(2)
var.add(3)
var.add(4)
var.add(5)
var.print_list()
print("-----")
var.remove(3)
var.print_list()
print("-----")
var.remove(2)
var.print_list()
print("-----")
var.remove(1)
var.print_list()
print("-----")
var.remove(4)
var.print_list()
print("-----")
var.remove(5)
var.print_list()
print("-----")
var.remove(1)
var.print_list()
print("-----")
var.add(1)
var.print_list()
print("-----")
var.remove(1)
var.print_list()