-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubly_linked_list.py
More file actions
50 lines (43 loc) · 1.68 KB
/
doubly_linked_list.py
File metadata and controls
50 lines (43 loc) · 1.68 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
class _DoublyLinkedBase:
"""A base class represetnting a doubly linked list"""
#--------------------------------------------------------------
class _Node:
"""Nonpublic class for string a doyubly linked list node"""
__slots__ = '_element', '_prev', '_next'
def __init__(self, element, previous, next):
self._element = element
self._prev = previous
self._next = next
#--------------------------------------------------------------
def __init__(self):
"""
Creates an empty list with head and tail sentinel nodes
"""
self._head = self._Node(None, None, None)
self._tail = self._Node(None, None, None)
self._head._next = self._tail
self._tail._prev = self._head
self._size = 0
def __len__(self):
"""returns: the number of elements in the list"""
return self._size
def is_empty(self):
"""returns: True if the list is empty, False otherwise"""
return self._size == 0
def _insert_between(self, e, predecessor, successor):
"""Add element e between two existing nodes and return the new nod"""
node = self._Node(e, predecessor, successor)
predecessor._next = node
successor._prev = node
self._size += 1
return node
def _delete_node(self, node):
"""Delete a non-sentinel node and return its element"""
predecessor = node._prev
successor = node._next
predecessor._next = successor
successor._prev = predecessor
self._size -= 1
element = node._element
node._prev = node._next = node._element = None
return element