Esempio n. 1
0
 def test_add_at_tail(self):
     dll = Dll()
     dll.add_at_head("apple")
     dll.add_at_head("orange")
     dll.add_at_tail("mango")
     assert str(dll.head) == "orange"
     assert dll.size_dll() == 3
Esempio n. 2
0
 def test_add_at_nth_position(self):
     dll = Dll()
     dll.add_at_head(1)
     dll.add_at_head(2)
     dll.add_at_head(3)
     dll.add_at_head(4)
     dll.add_at_nth_position(5, 0)
     size = dll.size_dll()
     assert size == 5
Esempio n. 3
0
 def test_size(self):
     dll = Dll()
     size = dll.size_dll()
     # Test an empty Linked List
     assert size == 0
     # Test Linked List with some nodes
     dll.add_at_head(1)
     dll.add_at_head(2)
     dll.add_at_head(3)
     dll.add_at_head(4)
     size = dll.size_dll()
     assert size == 4
Esempio n. 4
0
 def test_add_at_head(self):
     dll = Dll()
     dll.add_at_head("apple")
     dll.add_at_head("orange")
     assert str(dll.head) == "orange"
# Reverse doubly linked list
from doublylinkedlist.doubly_linkedlist import Dll


def reverse_dll(current):
    """Reverse a linked list using recursion."""
    if current.next is None:
        dll.head = current
        return dll.head
    reverse_dll(current.next)
    temp = current.next
    temp.next = current
    current.next = None


if __name__ == "__main__":
    dll = Dll()
    dll.add_at_head(1)
    dll.add_at_head(3)
    dll.add_at_head(5)
    dll.add_at_head(7)
    print("Elements of linked list:")
    dll.print_dll()
    print("Head:")
    print(dll.head)
    print("Elements of reversed linked list:")
    reverse_dll(dll.head)
    dll.print_dll()
    print("Head after reversing:")
    print(dll.head)