Ejemplo n.º 1
0
    def test_delete_from_1el(self):
        list4 = LinkedList()
        n1 = Node(55)
        list4.add_in_tail(n1)

        list4.delete(55)
        self.assertEqual(list4.get_all(), [])
        self.assertEqual(list4.get_head(), None)
        self.assertEqual(list4.get_tail(), None)
Ejemplo n.º 2
0
    def test_delete_from_list_with_all_equal_nodes(self):
        s_list_example = LinkedList()
        for i in range(20):
            s_list_example.add_in_tail(Node(2))

        s_list_example.delete(2, all=True)
        self.assertEqual(s_list_example.len(), 0)
        self.assertEqual(s_list_example.head, None)
        self.assertEqual(s_list_example.tail, None)
Ejemplo n.º 3
0
    def test_find_in_list_with_many_nodes(self):
        s_list_example = LinkedList()
        list_elements = [34, 45, 1, 2, 34, 6, 8, 12, 34]
        for i in list_elements:
            s_list_example.add_in_tail(Node(i))

        self.assertEqual(s_list_example.find_all(Node(555)), [])
        self.assertEqual(len(s_list_example.find_all(34)), 3)
        self.assertEqual(len(s_list_example.find_all(45)), 1)
Ejemplo n.º 4
0
    def test_delet_from_list_with_one_node(self):
        s_list_example = LinkedList()
        s_list_example.add_in_tail(Node(2))

        s_list_example.delete(2, all=True)

        self.assertEqual(s_list_example.len(), 0)
        self.assertEqual(s_list_example.head, None)
        self.assertEqual(s_list_example.tail, None)
Ejemplo n.º 5
0
    def test_find_all_not_empty(self):
        list2 = LinkedList()
        n1 = Node(55)
        n2 = Node(12)
        n3 = Node(55)
        list2.add_in_tail(n1)
        list2.add_in_tail(n2)
        list2.add_in_tail(n3)

        self.assertEqual(list2.find_all(n3.value), [n1, n3])
        self.assertEqual(list2.get_head(), n1)
        self.assertEqual(list2.get_tail(), n3)
Ejemplo n.º 6
0
def lists_add(list1, list2):
    if list1.len() != list2.len():
        return False
    else:
        result_list = LinkedList()
        node1 = list1.get_head()
        node2 = list2.get_head()
        while node1 != None:
            result_list.add_in_tail(Node(node1.value + node2.value))
            node1 = node1.next
            node2 = node2.next
        return result_list
Ejemplo n.º 7
0
    def test_mergeListsWithNonEqualLength(self):
        list1 = LinkedList()
        list2 = LinkedList()

        elements1 = [2, 3, 4, 5, 6, 7]
        elements2 = [5, 1, 2, 8, 11]

        for i in elements1:
            list1.add_in_tail(Node(i))

        for i in elements2:
            list2.add_in_tail(Node(i))

        self.assertEqual(mergeLists(list1, list2), [[], -1])
Ejemplo n.º 8
0
    def test_find_in_long_random_list(self):
        s_list_example = LinkedList()
        listLength = randint(10000, 1000000)
        valuesForFind = []

        for i in range(1, listLength):
            value = randint(1, 1000)
            s_list_example.add_in_tail(Node(value))
            valuesForFind.append(value)

        for i in range(100):
            index = randint(0, len(valuesForFind))
            value = valuesForFind[index]
            self.assertIsNotNone(s_list_example.find(value))
Ejemplo n.º 9
0
    def test_insert_to_body(self):
        list3 = LinkedList()
        n1 = Node(55)
        n2 = Node(12)
        n3 = Node(55)
        n4 = Node(10)
        list3.add_in_tail(n1)
        list3.add_in_tail(n2)
        list3.add_in_tail(n3)

        list3.insert(n2, n4)
        self.assertEqual(list3.get_all(), [
                         n1.value, n2.value, n4.value, n3.value])
        self.assertEqual(list3.get_head(), n1)
        self.assertEqual(list3.get_tail(), list3.find_all(n1.value)[1])
Ejemplo n.º 10
0
    def test_insert_head(self):
        list3 = LinkedList()
        n1 = Node(55)
        n2 = Node(12)
        n3 = Node(55)
        n4 = Node(10)
        n5 = Node(20)
        list3.add_in_tail(n1)
        list3.add_in_tail(n2)
        list3.add_in_tail(n3)
        list3.add_in_tail(n4)

        list3.insert(None, n5)
        self.assertEqual(list3.get_all(), [
                         n5.value, n1.value, n2.value, n3.value, n4.value])
        self.assertEqual(list3.get_head(), n5)
        self.assertEqual(list3.get_tail(), n4)
Ejemplo n.º 11
0
    def test_mergeListsWithEqualLength(self):
        list1 = LinkedList()
        list2 = LinkedList()

        elements1 = [2, 3, 4, 5, 6]
        elements2 = [5, 1, 2, 8, 11]
        testElementsSum = []

        for i in range(len(elements1)):
            testElementsSum.append(elements1[i] + elements2[i])

        for i in elements1:
            list1.add_in_tail(Node(i))

        for i in elements2:
            list2.add_in_tail(Node(i))

        self.assertEqual(mergeLists(list1, list2)[0], testElementsSum)
Ejemplo n.º 12
0
    def test_insert_in_list_with_one_node(self):
        s_list_example = LinkedList()
        s_list_example.add_in_tail(Node(5))

        s_list_example.insert(Node(5), Node(2))

        self.assertEqual(s_list_example.len(), 2)
        self.assertEqual(s_list_example.head.value, 5)
        self.assertEqual(s_list_example.tail.value, 2)
        self.assertNotEqual(s_list_example.head, s_list_example.tail)

        s_list_example = LinkedList()
        s_list_example.add_in_tail(Node(5))

        s_list_example.insert(None, Node(2))

        self.assertEqual(s_list_example.len(), 2)
        self.assertEqual(s_list_example.head.value, 2)
        self.assertEqual(s_list_example.tail.value, 5)
        self.assertNotEqual(s_list_example.head, s_list_example.tail)
Ejemplo n.º 13
0
    def test_add_in_tail(self):
        list3 = LinkedList()
        n1 = Node(55)
        n2 = Node(12)
        n3 = Node(55)

        list3.add_in_tail(n1)
        self.assertEqual(list3.get_all(), [55])
        self.assertEqual(list3.find(55), n1)
        self.assertEqual(list3.find_all(55), [n1])
        self.assertEqual(list3.get_head(), n1)
        self.assertEqual(list3.get_tail(), n1)

        list3.add_in_tail(n2)
        list3.add_in_tail(n3)
        self.assertEqual(list3.get_all(), [55, 12, 55])
        self.assertEqual(list3.find(55), n1)
        self.assertEqual(list3.find_all(55), [n1, n3])
        self.assertEqual(list3.get_head(), n1)
        self.assertEqual(list3.get_tail(), n3)
Ejemplo n.º 14
0
    def test_delete_from_short_list(self):
        s_list_example = LinkedList()
        s_list_example.add_in_tail(Node(5))
        s_list_example.add_in_tail(Node(1))
        s_list_example.add_in_tail(Node(6))
        s_list_example.add_in_tail(Node(8))
        s_list_example.add_in_tail(Node(11))
        s_list_example.add_in_tail(Node(77))

        s_list_example.delete(5)
        self.assertEqual(s_list_example.head.value, 1)
Ejemplo n.º 15
0
    def test_find_in_list(self):
        s_list_example = LinkedList()
        s_list_example.add_in_tail(Node(5))
        s_list_example.add_in_tail(Node(1))
        s_list_example.add_in_tail(Node(6))
        s_list_example.add_in_tail(Node(8))
        s_list_example.add_in_tail(Node(11))
        s_list_example.add_in_tail(Node(77))

        self.assertIsNotNone(s_list_example.find(5))
        self.assertIsNotNone(s_list_example.find(77))
        self.assertIsNotNone(s_list_example.find(6))
Ejemplo n.º 16
0
    def test_add_in_list(self):
        s_list_example = LinkedList()
        s_list_example.add_in_tail(Node(5))
        s_list_example.add_in_tail(Node(1))
        s_list_example.add_in_tail(Node(6))
        s_list_example.add_in_tail(Node(8))
        s_list_example.add_in_tail(Node(11))
        s_list_example.add_in_tail(Node(77))

        s_list = s_list_example
        s_list.add_in_tail(Node(88))
        self.assertEqual(s_list.len(), s_list_example.len())
Ejemplo n.º 17
0
    def test_insert_in_list(self):
        s_list_example = LinkedList()
        s_list_example.add_in_tail(Node(5))
        s_list_example.add_in_tail(Node(1))
        s_list_example.add_in_tail(Node(6))
        s_list_example.add_in_tail(Node(8))
        s_list_example.add_in_tail(Node(11))
        s_list_example.add_in_tail(Node(77))

        s_list_example.insert(Node(5), Node(2))

        self.assertEqual(s_list_example.head.next.value, 2)

        s_list_example.insert(None, Node(8))

        self.assertEqual(s_list_example.head.value, 8)

        s_list_example.insert(Node(77), Node(99))

        self.assertEqual(s_list_example.tail.value, 99)
Ejemplo n.º 18
0
    def test_find_all_in_1el(self):
        list2 = LinkedList()
        n1 = Node(55)
        list2.add_in_tail(n1)

        self.assertEqual(list2.find_all(n1.value), [n1])
Ejemplo n.º 19
0
    def test_find_in_list_with_one_node(self):
        s_list_example = LinkedList()
        s_list_example.add_in_tail(Node(34))

        self.assertEqual(s_list_example.find_all(Node(2)), [])
        self.assertEqual(len(s_list_example.find_all(34)), 1)
Ejemplo n.º 20
0
 def test_add_in_empty_list(self):
     s_list = LinkedList()
     s_list.add_in_tail(Node(5))
     self.assertEqual(s_list.len(), 1)