forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubly_linked_list.py
75 lines (50 loc) · 2 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
72
73
74
75
class Node:
def __init__(self, data = None, next = None, previous = None):
self.data = data
self.next = next
self.previous = previous
class Lista:
def __init__(self):
self.first = self.last = Node()
def empty(self):
return self.first.data == self.last.data
def search(self, data):
if self.empty(): return None
auxiliar = self.first.next
while auxiliar.next != None and auxiliar.data != data:
auxiliar = auxiliar.next
if auxiliar.data == data:
return auxiliar.data
return None
def append(self, data):
self.last.next = Node(data = data, next = None, previous = self.last)
self.last = self.last.next
def __str__(self):
if self.empty():
return ""
aux = self.first
format_ = ""
while aux.next != None:
if aux.data!= None:
format_ += str(aux.data) + " "
aux = aux.next
format_ += str(aux.data) + ""
return format_
def remove(self, data):
if self.empty(): return None
auxiliar = self.first.next
while auxiliar != None and auxiliar.data != data:
auxiliar = auxiliar.next
if auxiliar == None: return None
else:
item = auxiliar.data
if auxiliar.previous != None:
auxiliar.previous.next = auxiliar.next
if auxiliar.next != None:
auxiliar.next.previous = auxiliar.previous
if self.empty(): self.last = self.first = Node()
elif auxiliar.next == None: self.last = auxiliar.previous
del auxiliar
return item
#the auxiliar variable is to help like a flag in the code, like aux too
#If something isn't right, or in another language it is bc I am a native portuguese speaker, so I translated my code