2020-01-05 09:51:03 +00:00
|
|
|
"""
|
2020-05-22 06:10:11 +00:00
|
|
|
Implementing Deque using DoublyLinkedList ...
|
2020-01-05 09:51:03 +00:00
|
|
|
Operations:
|
|
|
|
1. insertion in the front -> O(1)
|
|
|
|
2. insertion in the end -> O(1)
|
2020-01-18 12:24:33 +00:00
|
|
|
3. remove from the front -> O(1)
|
2020-01-05 09:51:03 +00:00
|
|
|
4. remove from the end -> O(1)
|
|
|
|
"""
|
|
|
|
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
class _DoublyLinkedBase:
|
|
|
|
""" A Private class (to be inherited) """
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
class _Node:
|
2020-01-08 13:06:53 +00:00
|
|
|
__slots__ = "_prev", "_data", "_next"
|
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
def __init__(self, link_p, element, link_n):
|
|
|
|
self._prev = link_p
|
|
|
|
self._data = element
|
|
|
|
self._next = link_n
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
def has_next_and_prev(self):
|
2020-01-08 13:06:53 +00:00
|
|
|
return " Prev -> {0}, Next -> {1}".format(
|
2020-03-04 12:40:28 +00:00
|
|
|
self._prev is not None, self._next is not None
|
2020-01-08 13:06:53 +00:00
|
|
|
)
|
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
def __init__(self):
|
|
|
|
self._header = self._Node(None, None, None)
|
|
|
|
self._trailer = self._Node(None, None, None)
|
|
|
|
self._header._next = self._trailer
|
|
|
|
self._trailer._prev = self._header
|
|
|
|
self._size = 0
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
def __len__(self):
|
|
|
|
return self._size
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
def is_empty(self):
|
|
|
|
return self.__len__() == 0
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
def _insert(self, predecessor, e, successor):
|
|
|
|
# Create new_node by setting it's prev.link -> header
|
|
|
|
# setting it's next.link -> trailer
|
|
|
|
new_node = self._Node(predecessor, e, successor)
|
|
|
|
predecessor._next = new_node
|
|
|
|
successor._prev = new_node
|
|
|
|
self._size += 1
|
|
|
|
return self
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
def _delete(self, node):
|
|
|
|
predecessor = node._prev
|
|
|
|
successor = node._next
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
predecessor._next = successor
|
|
|
|
successor._prev = predecessor
|
|
|
|
self._size -= 1
|
|
|
|
temp = node._data
|
|
|
|
node._prev = node._next = node._data = None
|
|
|
|
del node
|
|
|
|
return temp
|
|
|
|
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
class LinkedDeque(_DoublyLinkedBase):
|
|
|
|
def first(self):
|
2020-09-10 08:31:26 +00:00
|
|
|
"""return first element
|
2020-01-05 09:51:03 +00:00
|
|
|
>>> d = LinkedDeque()
|
|
|
|
>>> d.add_first('A').first()
|
|
|
|
'A'
|
|
|
|
>>> d.add_first('B').first()
|
|
|
|
'B'
|
2020-01-08 13:06:53 +00:00
|
|
|
"""
|
2020-01-05 09:51:03 +00:00
|
|
|
if self.is_empty():
|
2020-01-08 13:06:53 +00:00
|
|
|
raise Exception("List is empty")
|
2020-01-05 09:51:03 +00:00
|
|
|
return self._header._next._data
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
def last(self):
|
2020-09-10 08:31:26 +00:00
|
|
|
"""return last element
|
2020-01-05 09:51:03 +00:00
|
|
|
>>> d = LinkedDeque()
|
|
|
|
>>> d.add_last('A').last()
|
|
|
|
'A'
|
|
|
|
>>> d.add_last('B').last()
|
|
|
|
'B'
|
|
|
|
"""
|
|
|
|
if self.is_empty():
|
2020-01-08 13:06:53 +00:00
|
|
|
raise Exception("List is empty")
|
2020-01-05 09:51:03 +00:00
|
|
|
return self._trailer._prev._data
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
# DEque Insert Operations (At the front, At the end)
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
def add_first(self, element):
|
2020-09-10 08:31:26 +00:00
|
|
|
"""insertion in the front
|
2020-01-05 09:51:03 +00:00
|
|
|
>>> LinkedDeque().add_first('AV').first()
|
|
|
|
'AV'
|
|
|
|
"""
|
|
|
|
return self._insert(self._header, element, self._header._next)
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
def add_last(self, element):
|
2020-09-10 08:31:26 +00:00
|
|
|
"""insertion in the end
|
2020-01-05 09:51:03 +00:00
|
|
|
>>> LinkedDeque().add_last('B').last()
|
|
|
|
'B'
|
|
|
|
"""
|
|
|
|
return self._insert(self._trailer._prev, element, self._trailer)
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
# DEqueu Remove Operations (At the front, At the end)
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
def remove_first(self):
|
2020-09-10 08:31:26 +00:00
|
|
|
"""removal from the front
|
2020-01-05 09:51:03 +00:00
|
|
|
>>> d = LinkedDeque()
|
|
|
|
>>> d.is_empty()
|
|
|
|
True
|
|
|
|
>>> d.remove_first()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
IndexError: remove_first from empty list
|
|
|
|
>>> d.add_first('A') # doctest: +ELLIPSIS
|
2020-09-28 21:41:04 +00:00
|
|
|
<data_structures.linked_list.deque_doubly.LinkedDeque object at ...
|
2020-01-05 09:51:03 +00:00
|
|
|
>>> d.remove_first()
|
|
|
|
'A'
|
|
|
|
>>> d.is_empty()
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
if self.is_empty():
|
2020-01-08 13:06:53 +00:00
|
|
|
raise IndexError("remove_first from empty list")
|
2020-01-05 09:51:03 +00:00
|
|
|
return self._delete(self._header._next)
|
2020-01-08 13:06:53 +00:00
|
|
|
|
2020-01-05 09:51:03 +00:00
|
|
|
def remove_last(self):
|
2020-09-10 08:31:26 +00:00
|
|
|
"""removal in the end
|
2020-01-05 09:51:03 +00:00
|
|
|
>>> d = LinkedDeque()
|
|
|
|
>>> d.is_empty()
|
|
|
|
True
|
|
|
|
>>> d.remove_last()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
IndexError: remove_first from empty list
|
|
|
|
>>> d.add_first('A') # doctest: +ELLIPSIS
|
2020-09-28 21:41:04 +00:00
|
|
|
<data_structures.linked_list.deque_doubly.LinkedDeque object at ...
|
2020-01-05 09:51:03 +00:00
|
|
|
>>> d.remove_last()
|
|
|
|
'A'
|
|
|
|
>>> d.is_empty()
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
if self.is_empty():
|
2020-01-08 13:06:53 +00:00
|
|
|
raise IndexError("remove_first from empty list")
|
2020-01-05 09:51:03 +00:00
|
|
|
return self._delete(self._trailer._prev)
|