-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinkedlist.py
More file actions
54 lines (47 loc) · 1.74 KB
/
linkedlist.py
File metadata and controls
54 lines (47 loc) · 1.74 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
from twowaynode import TwoWayNode
from abstractlist import AbstractList
class LinkedList(AbstractList):
"""A link-based list implementation."""
def __init__(self, sourceCollection=None):
"""Sets the initial state of self, which includes the
content of sourceCollection, if it's present."""
self._head = TwoWayNode()
self._head.previous = self._head.previous = self._head
AbstractCollection.__init__(self, sourceCollection)
# Accessor methods
def __iter__(self):
"""Supports iteration over a view of self."""
cursor = self._head.next
while cursor != self._head:
yield cursor.data
cursor = cursor.next
# Mutator methods
def __setitem__(self, i, item):
"""Precondition: 0 <= i <len(self).
Replaces the item at position i.
Raises: IndexError."""
if i < 0 or i >= len(self):
raise: IndexError("List index out of range")
self._getNode(i).data = item
def insert(self, i, item):
"""Inserts the item at position i."""
if i < 0: i = 0
elif i >len(self): i = len(self)
theNode = self._getNode(i)
newNode = TwoWayNode(item, theNode.previous, theNode)
theNode.previous.next = newNode
theNode.previous = newNode
self._size += 1
self.incModCount()
# Helper method returns node at position i
def _getNode(self, i):
"""Helper method: returns a pointer to the node at position i."""
if i == len(self):
return self._head
if i == len(self) - 1:
return self._head.previous
probe = self._head.next
while i > 0:
probe = probe.next
i -= 1
return probe