-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.py
162 lines (135 loc) · 4.67 KB
/
LinkedList.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
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
"""
MIT License
Copyright (c) 2023 Espérance AYIWAHOUN
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
class Node:
def __init__(self, value):
self.value = value
self.next = None
def __str__(self):
return f"value : {self.value}"
def __eq__(self, node: object):
return self.value == node.value
class LinkedList:
"""
Stocke les données sous la forme de liste chainée
"""
def __init__(self):
self.head = None
def __contain(self, value):
"""
Retourne le noeud si elle existe déjà dans la liste et un pointeur sur le noeud sinon
"""
current = self.head
while current:
if current.value == value:
return current
current = current.next
return False
def __contains__(self, value):
"""
Retourne le noeud si elle existe déjà dans la liste et False sinon
"""
if self.__contain(value) is not False:
return True
return False
def __str__(self):
liste = ""
current = self.head
while current:
if current.next:
liste += f" {current.value} -->"
else:
liste += f" {current.value}"
current = current.next
return liste
def append(self, value):
"""
Ajoute une nouvelle valeur unique à la fin liste chainée
"""
if self.head is None:
self.head = Node(value)
if self.__contain(value) is False:
new_node = Node(value)
current = self.head
while current.next:
current = current.next
current.next = new_node
return True
else:
return False
def insert(self, value, new_value, before=False):
"""
Insère par défaut une nouvelle valeur derrière la valeur donnée.
Si before est passé à True, il fait l'insertion devant
"""
node = self.__contain(value)
if node is False:
raise ValueError('La valeur saisie exite déjà.')
new_node = Node(new_value)
if before:
current = self.head
if current == node:
# Cas où il s'agit d'une insertion devant la tête de la liste chainée
new_node.next = self.head
self.head = new_node
return True
while current.next:
if current.next == node:
break
current = current.next
current.next = new_node
new_node.next = node
return True
else:
"""
Ici, la valeur est insérée juste après le noeud s'il existe
"""
new_node = Node(new_value)
new_node.next = node.next
node.next = new_node
return True
def remove(self, value):
"""
supprime le noeud s'il elle existe
"""
node_rm = self.__contain(value)
if node_rm is False:
return
current = self.head
if current == node_rm: # Au cas où l'élément à supprimer est la tête
self.head = current.next
del node_rm
return True
while current.next:
if current.next == node_rm:
break
current = current.next
current.next = current.next.next
del node_rm
return True
def size(self):
"""
Retourne le nombre d'éléments dans une liste chainée
"""
taille = 0
current = self.head
while current:
taille += 1
current = current.next
return taille