def test_reverse_iter(self):
     sll = Sll()
     sll.add_at_head(1)
     sll.add_at_head(2)
     sll.add_at_head(3)
     result = str(sll.head)
     assert result == str(3)
     sll.reverse_iter()
     assert str(sll.head) == str(1)
 def test_search(self):
     sll = Sll()
     # Test an empty Linked List
     assert sll.search(5) == 'Linked list is empty'
     # Test Linked List with some nodes
     sll.add_at_head(1)
     sll.add_at_head(2)
     sll.add_at_head(3)
     assert sll.search('python') is False
     assert sll.search(3) is True
 def test_remove(self):
     sll = Sll()
     # Test an empty Linked List
     assert sll.remove(5) == "Linked list is empty"
     # Test Linked List with some nodes
     sll.add_at_head(1)
     sll.add_at_head(2)
     sll.add_at_head(3)
     assert str(sll.remove(24)) == 'A Node with given data is not present.'
     assert sll.search(2) is True
     sll.remove(2)
     assert sll.search(2) is False
 def test_list_size(self):
     sll = Sll()
     size = sll.list_size()
     # Test an empty Linked List
     assert size == 0
     # Test Linked List with some nodes
     sll.add_at_head(1)
     sll.add_at_head(2)
     sll.add_at_head(3)
     sll.add_at_head(4)
     size = sll.list_size()
     assert size == 4
 def test_add_at_head(self):
     sll = Sll()
     sll.add_at_head('cpp')
     result = str(sll.head)
     expected = 'cpp'
     assert result == expected
Example #6
0
from singlylinkedlist.singly_linkedlist import Sll


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


if __name__ == "__main__":
    sll = Sll()
    sll.add_at_head(1)
    sll.add_at_head(3)
    sll.add_at_head(5)
    sll.add_at_head(7)
    print("Elements of linked list:")
    sll.print_list()
    print("Head:")
    print(sll.head)
    print("Elements of reversed linked list:")
    reverse_linked_list(sll.head)
    sll.print_list()
    print("Head after reversing:")
    print(sll.head)
Example #7
0
from singlylinkedlist.singly_linkedlist import Sll


def is_circular_linkedlist(input_list):
    current = input_list.head
    while current.next:
        current = current.next
        if current.next == input_list.head:
            return True
    return False


if __name__ == "__main__":

    s = Sll()
    s.add_at_head(1)
    s.add_at_head(2)
    s.add_at_head(3)
    s.add_at_head(4)

    cll = CSll()
    cll.add_at_first(1)
    cll.add_at_first(2)
    cll.add_at_first(3)
    cll.add_at_first(4)

    print(is_circular_linkedlist(s))
    print(is_circular_linkedlist(cll))