Beispiel #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
Beispiel #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
Beispiel #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
Beispiel #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"
Beispiel #5
0
 def test_is_empty(self):
     dll = Dll()
     assert dll.is_empty() is True
     node = DllNode(1)
     dll.head = node
     assert dll.is_empty() is False
# 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)

def printReverse(current):
    """Print all the elements of the list in reverse."""
    temp = current
    if current is None:
        return

    # Traverse to the end
    while current.next:
        current = current.next

    # Traverse backward
    while current:
        print(current.data)
        current = current.prev


if __name__ == "__main__":
    dll = Dll()
    dll.add_at_head(1)
    dll.add_at_head(3)
    dll.add_at_tail(5)
    dll.add_at_tail(7)
    print("Head of linked list:")
    print(dll.head)
    print("Forward:")
    printForward(dll.head)
    print("Reverse:")
    printReverse(dll.head)
Beispiel #8
0
                current.prev = None
                current = None
                return

            # case 4:
            else:
                prev_node = current.prev
                prev_node.next = None
                current.prev = None
                current = None
                return
        current = current.next


if __name__ == "__main__":
    dll = Dll()
    dll.add_at_tail(1)
    dll.add_at_tail(2)
    dll.add_at_tail(3)
    dll.add_at_tail(2)
    dll.add_at_tail(1)
    dll.add_at_tail(4)
    dll.add_at_tail(2)
    dll.add_at_tail(1)
    print("List: ")
    dll.print_dll()
    print("\n")
    print("List after removing duplicates: ")
    remove_duplicates(dll.head)
    dll.print_dll()
def print_forward(current):
    """Print all the elements of the linked list using recursion."""
    if current is None:
        return "Exit the linked list."
    print(current.data)
    print_forward(current.next)


def print_reverse(current):
    """Print all the elements of the linked list after reversing using recursion."""
    if current is None:
        return "Exit the linked list."
    print_reverse(current.next)
    print(current.data)


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:")
    print_forward(dll.head)
    print("Head of linked list:")
    print(dll.head)
    print("Elements of reversed linked list:")
    print_reverse(dll.head)
    print("Head of linked list:")
    print(dll.head)