mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
[bug fixing] Edge case of the double ended queue (#9823)
* fix the edge case of the double ended queue pop the last element * refactoring doc --------- Co-authored-by: Jiang15 <weijiang@weijiangs-MacBook-Pro.local>
This commit is contained in:
parent
6643c95537
commit
55ee273419
@ -242,12 +242,20 @@ class Deque:
|
|||||||
Removes the last element of the deque and returns it.
|
Removes the last element of the deque and returns it.
|
||||||
Time complexity: O(1)
|
Time complexity: O(1)
|
||||||
@returns topop.val: the value of the node to pop.
|
@returns topop.val: the value of the node to pop.
|
||||||
>>> our_deque = Deque([1, 2, 3, 15182])
|
>>> our_deque1 = Deque([1])
|
||||||
>>> our_popped = our_deque.pop()
|
>>> our_popped1 = our_deque1.pop()
|
||||||
>>> our_popped
|
>>> our_popped1
|
||||||
|
1
|
||||||
|
>>> our_deque1
|
||||||
|
[]
|
||||||
|
|
||||||
|
>>> our_deque2 = Deque([1, 2, 3, 15182])
|
||||||
|
>>> our_popped2 = our_deque2.pop()
|
||||||
|
>>> our_popped2
|
||||||
15182
|
15182
|
||||||
>>> our_deque
|
>>> our_deque2
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
|
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> deque_collections = deque([1, 2, 3, 15182])
|
>>> deque_collections = deque([1, 2, 3, 15182])
|
||||||
>>> collections_popped = deque_collections.pop()
|
>>> collections_popped = deque_collections.pop()
|
||||||
@ -255,17 +263,23 @@ class Deque:
|
|||||||
15182
|
15182
|
||||||
>>> deque_collections
|
>>> deque_collections
|
||||||
deque([1, 2, 3])
|
deque([1, 2, 3])
|
||||||
>>> list(our_deque) == list(deque_collections)
|
>>> list(our_deque2) == list(deque_collections)
|
||||||
True
|
True
|
||||||
>>> our_popped == collections_popped
|
>>> our_popped2 == collections_popped
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
# make sure the deque has elements to pop
|
# make sure the deque has elements to pop
|
||||||
assert not self.is_empty(), "Deque is empty."
|
assert not self.is_empty(), "Deque is empty."
|
||||||
|
|
||||||
topop = self._back
|
topop = self._back
|
||||||
|
# if only one element in the queue: point the front and back to None
|
||||||
|
# else remove one element from back
|
||||||
|
if self._front == self._back:
|
||||||
|
self._front = None
|
||||||
|
self._back = None
|
||||||
|
else:
|
||||||
self._back = self._back.prev_node # set new back
|
self._back = self._back.prev_node # set new back
|
||||||
# drop the last node - python will deallocate memory automatically
|
# drop the last node, python will deallocate memory automatically
|
||||||
self._back.next_node = None
|
self._back.next_node = None
|
||||||
|
|
||||||
self._len -= 1
|
self._len -= 1
|
||||||
@ -277,11 +291,17 @@ class Deque:
|
|||||||
Removes the first element of the deque and returns it.
|
Removes the first element of the deque and returns it.
|
||||||
Time complexity: O(1)
|
Time complexity: O(1)
|
||||||
@returns topop.val: the value of the node to pop.
|
@returns topop.val: the value of the node to pop.
|
||||||
>>> our_deque = Deque([15182, 1, 2, 3])
|
>>> our_deque1 = Deque([1])
|
||||||
>>> our_popped = our_deque.popleft()
|
>>> our_popped1 = our_deque1.pop()
|
||||||
>>> our_popped
|
>>> our_popped1
|
||||||
|
1
|
||||||
|
>>> our_deque1
|
||||||
|
[]
|
||||||
|
>>> our_deque2 = Deque([15182, 1, 2, 3])
|
||||||
|
>>> our_popped2 = our_deque2.popleft()
|
||||||
|
>>> our_popped2
|
||||||
15182
|
15182
|
||||||
>>> our_deque
|
>>> our_deque2
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> deque_collections = deque([15182, 1, 2, 3])
|
>>> deque_collections = deque([15182, 1, 2, 3])
|
||||||
@ -290,15 +310,21 @@ class Deque:
|
|||||||
15182
|
15182
|
||||||
>>> deque_collections
|
>>> deque_collections
|
||||||
deque([1, 2, 3])
|
deque([1, 2, 3])
|
||||||
>>> list(our_deque) == list(deque_collections)
|
>>> list(our_deque2) == list(deque_collections)
|
||||||
True
|
True
|
||||||
>>> our_popped == collections_popped
|
>>> our_popped2 == collections_popped
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
# make sure the deque has elements to pop
|
# make sure the deque has elements to pop
|
||||||
assert not self.is_empty(), "Deque is empty."
|
assert not self.is_empty(), "Deque is empty."
|
||||||
|
|
||||||
topop = self._front
|
topop = self._front
|
||||||
|
# if only one element in the queue: point the front and back to None
|
||||||
|
# else remove one element from front
|
||||||
|
if self._front == self._back:
|
||||||
|
self._front = None
|
||||||
|
self._back = None
|
||||||
|
else:
|
||||||
self._front = self._front.next_node # set new front and drop the first node
|
self._front = self._front.next_node # set new front and drop the first node
|
||||||
self._front.prev_node = None
|
self._front.prev_node = None
|
||||||
|
|
||||||
@ -432,3 +458,5 @@ if __name__ == "__main__":
|
|||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
dq = Deque([3])
|
||||||
|
dq.pop()
|
||||||
|
Loading…
Reference in New Issue
Block a user