2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2020-10-21 14:31:09 +00:00
|
|
|
https://en.wikipedia.org/wiki/Doubly_linked_list
|
|
|
|
"""
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-08-02 15:55:18 +00:00
|
|
|
|
2020-10-21 14:31:09 +00:00
|
|
|
class Node:
|
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data
|
|
|
|
self.previous = None
|
|
|
|
self.next = None
|
2020-08-02 15:55:18 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2020-10-21 14:31:09 +00:00
|
|
|
return f"{self.data}"
|
2020-08-02 15:55:18 +00:00
|
|
|
|
|
|
|
|
2020-10-21 14:31:09 +00:00
|
|
|
class DoublyLinkedList:
|
|
|
|
def __init__(self):
|
|
|
|
self.head = None
|
|
|
|
self.tail = None
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
"""
|
|
|
|
>>> linked_list = DoublyLinkedList()
|
|
|
|
>>> linked_list.insert_at_head('b')
|
|
|
|
>>> linked_list.insert_at_head('a')
|
|
|
|
>>> linked_list.insert_at_tail('c')
|
|
|
|
>>> tuple(linked_list)
|
|
|
|
('a', 'b', 'c')
|
|
|
|
"""
|
|
|
|
node = self.head
|
|
|
|
while node:
|
|
|
|
yield node.data
|
|
|
|
node = node.next
|
2020-08-02 15:55:18 +00:00
|
|
|
|
2020-10-21 14:31:09 +00:00
|
|
|
def __str__(self):
|
|
|
|
"""
|
|
|
|
>>> linked_list = DoublyLinkedList()
|
|
|
|
>>> linked_list.insert_at_tail('a')
|
|
|
|
>>> linked_list.insert_at_tail('b')
|
|
|
|
>>> linked_list.insert_at_tail('c')
|
|
|
|
>>> str(linked_list)
|
|
|
|
'a->b->c'
|
|
|
|
"""
|
|
|
|
return "->".join([str(item) for item in self])
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
"""
|
|
|
|
>>> linked_list = DoublyLinkedList()
|
|
|
|
>>> for i in range(0, 5):
|
|
|
|
... linked_list.insert_at_nth(i, i + 1)
|
|
|
|
>>> len(linked_list) == 5
|
|
|
|
True
|
|
|
|
"""
|
2023-04-01 06:26:43 +00:00
|
|
|
return sum(1 for _ in self)
|
2020-08-02 15:55:18 +00:00
|
|
|
|
2020-10-21 14:31:09 +00:00
|
|
|
def insert_at_head(self, data):
|
|
|
|
self.insert_at_nth(0, data)
|
2020-08-02 15:55:18 +00:00
|
|
|
|
|
|
|
def insert_at_tail(self, data):
|
2020-10-21 14:31:09 +00:00
|
|
|
self.insert_at_nth(len(self), data)
|
|
|
|
|
|
|
|
def insert_at_nth(self, index: int, data):
|
|
|
|
"""
|
|
|
|
>>> linked_list = DoublyLinkedList()
|
|
|
|
>>> linked_list.insert_at_nth(-1, 666)
|
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
....
|
2020-10-21 14:31:09 +00:00
|
|
|
IndexError: list index out of range
|
|
|
|
>>> linked_list.insert_at_nth(1, 666)
|
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
....
|
2020-10-21 14:31:09 +00:00
|
|
|
IndexError: list index out of range
|
|
|
|
>>> linked_list.insert_at_nth(0, 2)
|
|
|
|
>>> linked_list.insert_at_nth(0, 1)
|
|
|
|
>>> linked_list.insert_at_nth(2, 4)
|
|
|
|
>>> linked_list.insert_at_nth(2, 3)
|
|
|
|
>>> str(linked_list)
|
|
|
|
'1->2->3->4'
|
|
|
|
>>> linked_list.insert_at_nth(5, 5)
|
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
....
|
2020-10-21 14:31:09 +00:00
|
|
|
IndexError: list index out of range
|
|
|
|
"""
|
2023-04-01 12:23:21 +00:00
|
|
|
length = len(self)
|
|
|
|
|
|
|
|
if not 0 <= index <= length:
|
2020-10-21 14:31:09 +00:00
|
|
|
raise IndexError("list index out of range")
|
2020-08-02 15:55:18 +00:00
|
|
|
new_node = Node(data)
|
2020-10-21 14:31:09 +00:00
|
|
|
if self.head is None:
|
|
|
|
self.head = self.tail = new_node
|
|
|
|
elif index == 0:
|
|
|
|
self.head.previous = new_node
|
|
|
|
new_node.next = self.head
|
2020-08-02 15:55:18 +00:00
|
|
|
self.head = new_node
|
2023-04-01 12:23:21 +00:00
|
|
|
elif index == length:
|
2020-08-02 15:55:18 +00:00
|
|
|
self.tail.next = new_node
|
|
|
|
new_node.previous = self.tail
|
|
|
|
self.tail = new_node
|
2020-10-21 14:31:09 +00:00
|
|
|
else:
|
|
|
|
temp = self.head
|
2023-08-29 13:18:10 +00:00
|
|
|
for _ in range(index):
|
2020-10-21 14:31:09 +00:00
|
|
|
temp = temp.next
|
|
|
|
temp.previous.next = new_node
|
|
|
|
new_node.previous = temp.previous
|
|
|
|
new_node.next = temp
|
|
|
|
temp.previous = new_node
|
|
|
|
|
|
|
|
def delete_head(self):
|
|
|
|
return self.delete_at_nth(0)
|
|
|
|
|
|
|
|
def delete_tail(self):
|
|
|
|
return self.delete_at_nth(len(self) - 1)
|
|
|
|
|
|
|
|
def delete_at_nth(self, index: int):
|
|
|
|
"""
|
|
|
|
>>> linked_list = DoublyLinkedList()
|
|
|
|
>>> linked_list.delete_at_nth(0)
|
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
....
|
2020-10-21 14:31:09 +00:00
|
|
|
IndexError: list index out of range
|
|
|
|
>>> for i in range(0, 5):
|
|
|
|
... linked_list.insert_at_nth(i, i + 1)
|
|
|
|
>>> linked_list.delete_at_nth(0) == 1
|
|
|
|
True
|
|
|
|
>>> linked_list.delete_at_nth(3) == 5
|
|
|
|
True
|
|
|
|
>>> linked_list.delete_at_nth(1) == 3
|
|
|
|
True
|
|
|
|
>>> str(linked_list)
|
|
|
|
'2->4'
|
|
|
|
>>> linked_list.delete_at_nth(2)
|
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
....
|
2020-10-21 14:31:09 +00:00
|
|
|
IndexError: list index out of range
|
|
|
|
"""
|
2023-04-01 12:23:21 +00:00
|
|
|
length = len(self)
|
|
|
|
|
|
|
|
if not 0 <= index <= length - 1:
|
2020-10-21 14:31:09 +00:00
|
|
|
raise IndexError("list index out of range")
|
|
|
|
delete_node = self.head # default first node
|
2023-04-01 12:23:21 +00:00
|
|
|
if length == 1:
|
2020-10-21 14:31:09 +00:00
|
|
|
self.head = self.tail = None
|
|
|
|
elif index == 0:
|
|
|
|
self.head = self.head.next
|
|
|
|
self.head.previous = None
|
2023-04-01 12:23:21 +00:00
|
|
|
elif index == length - 1:
|
2020-10-21 14:31:09 +00:00
|
|
|
delete_node = self.tail
|
2020-08-02 15:55:18 +00:00
|
|
|
self.tail = self.tail.previous
|
|
|
|
self.tail.next = None
|
2020-10-21 14:31:09 +00:00
|
|
|
else:
|
|
|
|
temp = self.head
|
2023-08-29 13:18:10 +00:00
|
|
|
for _ in range(index):
|
2020-10-21 14:31:09 +00:00
|
|
|
temp = temp.next
|
|
|
|
delete_node = temp
|
|
|
|
temp.next.previous = temp.previous
|
|
|
|
temp.previous.next = temp.next
|
|
|
|
return delete_node.data
|
2020-08-02 15:55:18 +00:00
|
|
|
|
|
|
|
def delete(self, data) -> str:
|
2018-10-19 12:48:28 +00:00
|
|
|
current = self.head
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2020-08-02 15:55:18 +00:00
|
|
|
while current.data != data: # Find the position to delete
|
|
|
|
if current.next:
|
|
|
|
current = current.next
|
|
|
|
else: # We have reached the end an no value matches
|
2022-11-06 14:54:44 +00:00
|
|
|
raise ValueError("No data matching given value")
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if current == self.head:
|
2020-08-02 15:55:18 +00:00
|
|
|
self.delete_head()
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
elif current == self.tail:
|
2020-08-02 15:55:18 +00:00
|
|
|
self.delete_tail()
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
else: # Before: 1 <--> 2(current) <--> 3
|
|
|
|
current.previous.next = current.next # 1 --> 3
|
|
|
|
current.next.previous = current.previous # 1 <--> 3
|
2020-08-02 15:55:18 +00:00
|
|
|
return data
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2020-10-21 14:31:09 +00:00
|
|
|
def is_empty(self):
|
|
|
|
"""
|
|
|
|
>>> linked_list = DoublyLinkedList()
|
|
|
|
>>> linked_list.is_empty()
|
|
|
|
True
|
|
|
|
>>> linked_list.insert_at_tail(1)
|
|
|
|
>>> linked_list.is_empty()
|
|
|
|
False
|
|
|
|
"""
|
|
|
|
return len(self) == 0
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2020-10-21 14:31:09 +00:00
|
|
|
def test_doubly_linked_list() -> None:
|
|
|
|
"""
|
|
|
|
>>> test_doubly_linked_list()
|
|
|
|
"""
|
|
|
|
linked_list = DoublyLinkedList()
|
|
|
|
assert linked_list.is_empty() is True
|
|
|
|
assert str(linked_list) == ""
|
|
|
|
|
|
|
|
try:
|
|
|
|
linked_list.delete_head()
|
2023-05-26 07:34:17 +00:00
|
|
|
raise AssertionError # This should not happen.
|
2020-10-21 14:31:09 +00:00
|
|
|
except IndexError:
|
|
|
|
assert True # This should happen.
|
|
|
|
|
|
|
|
try:
|
|
|
|
linked_list.delete_tail()
|
2023-05-26 07:34:17 +00:00
|
|
|
raise AssertionError # This should not happen.
|
2020-10-21 14:31:09 +00:00
|
|
|
except IndexError:
|
|
|
|
assert True # This should happen.
|
|
|
|
|
|
|
|
for i in range(10):
|
|
|
|
assert len(linked_list) == i
|
|
|
|
linked_list.insert_at_nth(i, i + 1)
|
|
|
|
assert str(linked_list) == "->".join(str(i) for i in range(1, 11))
|
|
|
|
|
|
|
|
linked_list.insert_at_head(0)
|
|
|
|
linked_list.insert_at_tail(11)
|
2023-08-29 13:18:10 +00:00
|
|
|
assert str(linked_list) == "->".join(str(i) for i in range(12))
|
2020-10-21 14:31:09 +00:00
|
|
|
|
|
|
|
assert linked_list.delete_head() == 0
|
|
|
|
assert linked_list.delete_at_nth(9) == 10
|
|
|
|
assert linked_list.delete_tail() == 11
|
|
|
|
assert len(linked_list) == 9
|
|
|
|
assert str(linked_list) == "->".join(str(i) for i in range(1, 10))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from doctest import testmod
|
|
|
|
|
|
|
|
testmod()
|