Add Python doctests to doubly linked list (#170)

* Add Python doctests to doubly linked list

* Update doubly.py

* Update doubly.py

* Rename linked-lists/Python/doubly.py to algorithms/Python/linked_lists/doubly.py
pull/214/head
Christian Clauss 2021-04-16 14:45:17 +02:00 committed by GitHub
parent 261739f943
commit a22bea6fa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 0 deletions

View File

@ -5,6 +5,10 @@ class Node:
self.prev = None
def __repr__(self):
"""
>>> Node(77)
Node(77)
"""
return f"Node({self.data})"
@ -13,12 +17,34 @@ class DoublyLinkedList:
self.head = None
def __iter__(self):
"""
>>> dll = DoublyLinkedList()
>>> list(dll)
[]
>>> dll.push(88)
>>> tuple(dll)
(88,)
>>> dll.push(89)
>>> tuple(dll)
(89, 88)
"""
node = self.head
while node:
yield node.data
node = node.next
def __repr__(self):
"""
>>> dll = DoublyLinkedList()
>>> repr(dll)
'DoublyLinkedList()'
>>> dll.push(99)
>>> dll.push(100)
>>> repr(dll)
'DoublyLinkedList(100, 99)'
>>> str(dll)
'DoublyLinkedList(100, 99)'
"""
return f"DoublyLinkedList({', '.join(str(node) for node in self)})"
# Adding data elements is like pushing onto a stack