-
Notifications
You must be signed in to change notification settings - Fork 6
/
10-list.py
44 lines (37 loc) · 1.08 KB
/
10-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
from node import Node
class List:
def __init__(self):
self.head = None
self.count = 0
def is_empty(self):
return self.count == 0
def reset(self):
self.__init__()
def __len__(self):
return self.count
def _get_node(self, index):
if index < 0 or index > self.count:
raise Exception("Index out of bounds!")
node = self.head
for _ in range(index):
node = node.next
return node
def insert(self, index, item):
index = index % len(self)
if index == 0:
self.head = Node(item, self.head)
else:
node = self._get_node(index-1)
node.next = Node(item, node.next)
self.count += 1
def delete(self, index):
if self.is_empty():
raise IndexError("The list is empty!")
if index < 0 or index >= len(self):
raise IndexError("Index is out of range!")
if index == 0:
self.head = self.head.next
else:
node = self._get_node(index-1)
node.next = node.next.next
self.count -= 1