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.pypull/214/head
parent
261739f943
commit
a22bea6fa6
|
@ -5,6 +5,10 @@ class Node:
|
||||||
self.prev = None
|
self.prev = None
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
"""
|
||||||
|
>>> Node(77)
|
||||||
|
Node(77)
|
||||||
|
"""
|
||||||
return f"Node({self.data})"
|
return f"Node({self.data})"
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,12 +17,34 @@ class DoublyLinkedList:
|
||||||
self.head = None
|
self.head = None
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
|
"""
|
||||||
|
>>> dll = DoublyLinkedList()
|
||||||
|
>>> list(dll)
|
||||||
|
[]
|
||||||
|
>>> dll.push(88)
|
||||||
|
>>> tuple(dll)
|
||||||
|
(88,)
|
||||||
|
>>> dll.push(89)
|
||||||
|
>>> tuple(dll)
|
||||||
|
(89, 88)
|
||||||
|
"""
|
||||||
node = self.head
|
node = self.head
|
||||||
while node:
|
while node:
|
||||||
yield node.data
|
yield node.data
|
||||||
node = node.next
|
node = node.next
|
||||||
|
|
||||||
def __repr__(self):
|
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)})"
|
return f"DoublyLinkedList({', '.join(str(node) for node in self)})"
|
||||||
|
|
||||||
# Adding data elements is like pushing onto a stack
|
# Adding data elements is like pushing onto a stack
|
||||||
|
|
Loading…
Reference in New Issue