예제 #1
0
class TestLinkedList(unittest.TestCase):
    """Test case used to test methods from LinkedList class"""


    def setUp(self):
        """Test initialization"""
        self.liste = LinkedList()


    def test_newlist_empty(self):
        """2.1 test to check if a new created list is empty"""
        #print(self.liste.empty_list())
        #assertTrue checks if the empty_list() method returns True
        self.assertTrue(self.liste.empty_list())

    def test_add_notempty(self):
        """2.2 test to check that a list is not empty
        if if we've just added an element """
        new_node = Node("x")
        self.liste.add_first(new_node)
        #in this case we check that the list is not empty
        self.assertTrue(not self.liste.empty_list())


    def test_unchanged(self):
        """2.3 test if when we add and remove a new element,
        the linked list remains unchanged"""
        #list creation [a,b]
        node_a, node_b = "a", "b"
        self.liste.add_first(Node(node_a))
        self.liste.add_after(node_a, Node(node_b))
        #store the created list in a variable to be able to compared it later
        first_list = self.liste
        node_d = "d"
        self.liste.add_last(Node(node_d))
        self.liste.remove_node(node_d)
        #assertEqual checks that two given objects are equal
        self.assertEqual(self.liste, first_list)


    def test_add_first(self):
        """2.4 test if an element added at the first position of the list
        is the head of the list"""
        node_e, node_b, node_c = "e", "b", "c"
        self.liste.add_first(Node(node_b))
        self.liste.add_after(node_b, Node(node_c))
        #add the new head node e
        self.liste.add_first(Node(node_e))
        #comparison between the head of the list and the element inserted at the beginning
        self.assertEqual(self.liste.head.__repr__(), node_e)
def merge_lists(linked_list_1, linked_list_2):
    """

    """
    list_one_len = 0
    list_two_len = 0

    try:
        if linked_list_1.head:
            current_1 = linked_list_1.head
    except:
        return linked_list_2

    try:
        if linked_list_2.head:
            current_2 = linked_list_2.head
    except:
        return linked_list_1

    while current_1._next:
        list_one_len += 1

    while current_2._next:
        list_two_len += 1

    current_1 = linked_list_1.head
    current_2 = linked_list_2.head

    while i in range(list_one_len):
        if current_1 and current_2:
            ll.add_after(current_1, current_2)
            current_1 = current_1._next
            current_2 = current_2._next
        elif current_2 and not current_1:
            ll.add_before(current_1, current_2)
        else:
            return
llist.add_first(Node('y'))
print(llist)

# inserting an element to the last of the linked list
llist.add_last(Node('f'))
llist.add_last(Node('g'))
print(llist)

# inserting a node in between
# adding to an empty linked list
llist = LinkedList()
# llist.add_after('a', Node('b')) # exception
print(llist)
# adding an element after a target node
llist = LinkedList(list('abcde'))
llist.add_after('c', Node('cc'))
print(llist)
# attempt to add after a non-existent target node
# llist.add_after('f', Node('g')) # exception
print(llist)

# inserting a node before a target node
llist.add_before('c', Node('bb'))
print(llist)
llist.add_before('a', Node('zz'))
print(llist)

# removing a node
llist.remove_node('zz')
print(llist)
llist.remove_node('cc')